Javascript Ajax错误抛出:内部服务器错误,但状态为4
我下面有一个ajax调用Javascript Ajax错误抛出:内部服务器错误,但状态为4,javascript,c#,asp.net-mvc,asp.net-ajax,Javascript,C#,Asp.net Mvc,Asp.net Ajax,我下面有一个ajax调用 $(function(){ $.ajax({ type: "GET", url: '/CafeTableDetails/GetTotalItems', data: '{"url":"test"}', dataType: "json", contentType: "application/
$(function(){
$.ajax({
type: "GET",
url: '/CafeTableDetails/GetTotalItems',
data: '{"url":"test"}',
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (result) {
debugger
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
debugger
alert("some error");
}
});
});
它可以在C#MVC/CafeTableDetails/GetTotalItems
public ActionResult GetTotalItems()
{
bool isSuccess = true;
return Json(new { isUpdateSuccess = isSuccess, JsonRequestBehavior.AllowGet });
}
但是,当action方法将其作为Json返回时,它会陷入
错误中。XMLHttpRequest
返回状态为4,textStatus
=“Error”和errorshown
为“内部服务器错误”。如何进一步排除故障?您的ajax方法应该是:
$.ajax({
type: "GET",
url: "/CafeTableDetails/GetTotalItems",
data: JSON.stringify({ url: 'test' }),
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(response) {
console.log(response);
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.responseText);
}
});
您的控制器操作方法将是:
[HttpGet]
public JsonResult GetTotalItems(string url)
{
// some business logic and return
return Json(url, JsonRequestBehavior.AllowGet);
}
您的ajax方法应该是:
$.ajax({
type: "GET",
url: "/CafeTableDetails/GetTotalItems",
data: JSON.stringify({ url: 'test' }),
dataType: "json",
contentType: 'application/json; charset=utf-8',
success: function(response) {
console.log(response);
},
error: function(xhr, ajaxOptions, thrownError) {
console.log(xhr.responseText);
}
});
您的控制器操作方法将是:
[HttpGet]
public JsonResult GetTotalItems(string url)
{
// some business logic and return
return Json(url, JsonRequestBehavior.AllowGet);
}
您不需要返回JsonResult类型谢谢。虽然我不知道我做错了什么,但你的代码仍然有效。你不需要返回JsonResult类型谢谢。虽然我不知道我做错了什么,但你的代码仍然有效。服务器端函数真的被调用了吗?是的,我的服务器端函数被调用了。但是,当它返回到视图时,会出现“内部服务器错误”。服务器端函数是否真的被调用?是的,我的服务器端函数被调用。但是,当它将其返回到视图时,会出现“内部服务器错误”。