[英]Ajax call returns internal 500 error
Hi I am trying to execute an ajax call to the server but I seem to have no luck I keep getting back on the console an error 500 Internal server error.This is my ajax call: 嗨,我正在尝试执行对服务器的Ajax调用,但我似乎没有运气,我一直在控制台上返回错误500 Internal server error。这是我的Ajax调用:
$("body").on("click", "#ok", function (e) {
var id = $(this).attr("data-bookid");
$.ajax({
url: "/ProductEditor/DeleteBook",
type:"POST",
data: { bookId: parseInt(id) },
success: function (data) {
console.log(data);
},
error: function (data) {
console.log(data);
}
});
e.preventDefault();
})
And this is the C# code I am trying to call: 这是我要调用的C#代码:
public ActionResult DeleteBook(int bookId)
{
bookRepository.DeleteBook(bookId);
return RedirectToAction("Books", "ProductManager");
}
While trying to debug I have noticed that the DeleteBook method is not even called. 在尝试调试时,我注意到甚至没有调用DeleteBook方法。
What am I doing wrong? 我究竟做错了什么?
EDIT I have added the [HttpPost] attribute but it still does not work 编辑我添加了[HttpPost]属性,但仍然无法正常工作
Without more information, it's not easy to tell you what you are doing wrong. 没有更多信息,很难告诉您您做错了什么。
However, the error
function callback for the jQuery ajax function takes three parameters: 但是,jQuery ajax函数的
error
函数回调具有三个参数:
(jqXHR, textStatus, errorThrown) (jqXHR,textStatus,errorThrown)
$.ajax({
url: "/ProductEditor/DeleteBook",
type:"POST",
data: { bookId: parseInt(id) },
success: function (data) {
console.log(data);
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(errorThrown);
}
});
The third parameter, errorThrown
will have the HTTP response text for the call to /ProductEditor/DeleteBook
. 第三个参数
errorThrown
将具有对/ProductEditor/DeleteBook
的调用的HTTP响应文本。 I would suggest looking at what errorThrown is showing, as well as possibly running the entire program in debug mode. 我建议查看一下errorThrown显示的内容,以及可能在调试模式下运行整个程序。
Perhaps var id = $(this).attr("data-bookid");
也许
var id = $(this).attr("data-bookid");
is incorrect, and the value of id is being set to undefined
? 是不正确的,并且id的值被设置为
undefined
?
Also, you can use the Network tab in the F12 Development Tools in IE or Chrome, Firebug in Firefox, or Fiddler to debug ajax calls. 另外,您可以使用IE或Chrome中的F12开发工具中的“网络”选项卡,Firefox中的Firebug或Fiddler来调试ajax调用。 You can watch the ajax request being sent to the server as well as the response from the server in order to see whether the correct data (
bookId
) is being sent in the correct format (JSON) and what the response is from the server 您可以查看正在发送到服务器的ajax请求以及来自服务器的响应,以查看是否以正确的格式(JSON)发送了正确的数据(
bookId
)以及来自服务器的响应是什么
Add HttpPost to your method and try 将HttpPost添加到您的方法并尝试
[HttpPost]
public ActionResult DeleteBook(int bookId)
{
bookRepository.DeleteBook(bookId);
return RedirectToAction("Books", "ProductManager");
}
Your WebMethod must be static 您的WebMethod必须是静态的
[WebMethod(true)]
public static ActionResult DeleteBook(int bookId)
{
bookRepository.DeleteBook(bookId);
return RedirectToAction("Books", "ProductManager");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.