I tried to establish ajax request to asp.net mvc controller , but it give me internal server error
我试图建立对asp.net mvc控制器的ajax请求,但它给我内部服务器错误
// My Products Controller
[HttpPost]
public ActionResult FilterCategeory(int prodID)
{
var categs = new Categ() {PROD_ID=prodID }.Search();
return Json(categs);
}
//My ajax request
$("#categs").empty();
var prm = $("#prods").val();
$.ajax({
type: "POST",
url: '@Url.Action("FilterCategeory", "Products")',
contentType: "application/json; charset=utf-8",
data: {prodID: prm },
dataType: "json",
success: function (data)
{
alert('Success');
},
error: function () { alert('error');}
});
1 个解决方案
#1
The ajax request throws Invalid JSON primitive exception. So Pass the data using JSON.stringify(obj)
ajax请求抛出Invalid JSON原语异常。所以使用JSON.stringify(obj)传递数据
Ajax Request
var prm = $("#prods").val();
var obj = { prodID: prm };
$.ajax({
type: "POST",
url: '@Url.Action("FilterCategeory", "Home")',
contentType: "application/json; charset=utf-8",
data : JSON.stringify(obj),
dataType: "json",
success: function (data) {
alert('Success');
},
error: function () { alert('error'); }
});
Check this question hope it will help you.
检查这个问题希望它能帮到你。
You can check the error type in Firefox or Chrome In firefox
您可以在Firefox或Chrome中检查错误类型
Right click the browser click Inspect Element. Then selected the Network tab. When you click the request it will show header, cookies etc. From that choose Response. So you can found the error
右键单击浏览器,单击“检查元素”。然后选择“网络”选项卡。当您单击该请求时,它将显示标题,cookie等。从中选择响应。所以你可以找到错误
In chrome
#1
The ajax request throws Invalid JSON primitive exception. So Pass the data using JSON.stringify(obj)
ajax请求抛出Invalid JSON原语异常。所以使用JSON.stringify(obj)传递数据
Ajax Request
var prm = $("#prods").val();
var obj = { prodID: prm };
$.ajax({
type: "POST",
url: '@Url.Action("FilterCategeory", "Home")',
contentType: "application/json; charset=utf-8",
data : JSON.stringify(obj),
dataType: "json",
success: function (data) {
alert('Success');
},
error: function () { alert('error'); }
});
Check this question hope it will help you.
检查这个问题希望它能帮到你。
You can check the error type in Firefox or Chrome In firefox
您可以在Firefox或Chrome中检查错误类型
Right click the browser click Inspect Element. Then selected the Network tab. When you click the request it will show header, cookies etc. From that choose Response. So you can found the error
右键单击浏览器,单击“检查元素”。然后选择“网络”选项卡。当您单击该请求时,它将显示标题,cookie等。从中选择响应。所以你可以找到错误
In chrome