尝试调用我的 Controller 的方法时出现错误( Controller 的名称是 ProductsController
):
public ActionResult GetProducts()
{
return false;
}
调用代码如下:
$(document).ready(function () {
$.ajax({
type: 'POST',
url: '@Url.Action("GetProducts", "ProductsController")',
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf8',
data: JSON.stringify(""),
})
Chrome 控制台显示:
jquery-1.10.2.js:8720 POST http ://localhost:56408/ProductsController/GetProducts 404 (Not Found)
你知道问题出在哪里吗?
最佳答案
使用 Controller 名称前缀Products
而不是ProductsController
$(document).ready(function () {
$.ajax({
type: 'POST',
url: '@Url.Action("GetProducts", "Products")',
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf8',
data: JSON.stringify(""),
});
Asp.Net-MVC 使用 Controller 的命名约定。
关于c# - 通过ajax调用 Controller 方法到达 "404 not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38805282/