Ajax 将字符串发布到控制器操作返回 404

问题描述 投票:0回答:1

在 keyup 上,我调用 JavaScript 函数,该函数应该调用控制器操作并返回结果,但我收到 404 错误。我在控制器操作的顶部有断点,但从未击中任何断点。我认为它没有进入,因为类型或与发送的数据有关。
它是用 C# MVC .NET 6 编码的

Java Script::::
function GetThingsContaining() { 

    $.ajax({
        type: "POST",
        url: "GetThingsContaining/MyController",
        data: { searchCriteria: "afd" },
        dataType: "json",
        success: function (data) {
            //do work if successful
        },
        error: function (data) {
            alert("Well that stinks")
        },
    }); 
}
   Controller Action
        [HttpPost]
        public IActionResult GetThingsContaining(string searchCriteria) {
            return PartialView("~/Views/Investments/_SearchDataList.cshtml", returnModel);
        }

我在返回时设置了一个断点,但从未击中它。在开发工具中它给了我一个 404

javascript c# .net ajax model-view-controller
1个回答
0
投票

试试这个,在控制器中使用[Frombody],然后尝试像这样请求

fetch('GetThingsContaining/MyController', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ searchCriteria: 'afd' }),
    })
    .then(response => {
        if (!response.ok) {
            throw new Error('Network response was not ok');
        }
        return response.json();
    })
    .then(data => {
        // do work if successful
    })
© www.soinside.com 2019 - 2024. All rights reserved.