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内部服务器错误。这是我的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]属性,但它仍然无法正常工作
3 个解决方案
#1
2
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函数的错误函数回调有三个参数:
(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
. I would suggest looking at what errorThrown is showing, as well as possibly running the entire program in debug mode.
第三个参数errorThrown将具有对/ ProductEditor / DeleteBook的调用的HTTP响应文本。我建议查看errorThrown显示的内容,以及可能在调试模式下运行整个程序。
Perhaps var id = $(this).attr("data-bookid");
is incorrect, and the value of id is being set to undefined
?
也许var id = $(this).attr(“data-bookid”);是不正确的,并且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. 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
此外,您可以使用IE或Chrome中的F12开发工具中的“网络”选项卡,Firefox中的Firebug或Fiddler来调试ajax调用。您可以查看发送到服务器的ajax请求以及来自服务器的响应,以查看是否以正确的格式(JSON)发送了正确的数据(bookId)以及响应来自服务器的内容
#2
0
Add HttpPost to your method and try
将HttpPost添加到您的方法并尝试
[HttpPost]
public ActionResult DeleteBook(int bookId)
{
bookRepository.DeleteBook(bookId);
return RedirectToAction("Books", "ProductManager");
}
#3
0
Your WebMethod must be static
您的WebMethod必须是静态的
[WebMethod(true)]
public static ActionResult DeleteBook(int bookId)
{
bookRepository.DeleteBook(bookId);
return RedirectToAction("Books", "ProductManager");
}
#1
2
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函数的错误函数回调有三个参数:
(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
. I would suggest looking at what errorThrown is showing, as well as possibly running the entire program in debug mode.
第三个参数errorThrown将具有对/ ProductEditor / DeleteBook的调用的HTTP响应文本。我建议查看errorThrown显示的内容,以及可能在调试模式下运行整个程序。
Perhaps var id = $(this).attr("data-bookid");
is incorrect, and the value of id is being set to undefined
?
也许var id = $(this).attr(“data-bookid”);是不正确的,并且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. 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
此外,您可以使用IE或Chrome中的F12开发工具中的“网络”选项卡,Firefox中的Firebug或Fiddler来调试ajax调用。您可以查看发送到服务器的ajax请求以及来自服务器的响应,以查看是否以正确的格式(JSON)发送了正确的数据(bookId)以及响应来自服务器的内容
#2
0
Add HttpPost to your method and try
将HttpPost添加到您的方法并尝试
[HttpPost]
public ActionResult DeleteBook(int bookId)
{
bookRepository.DeleteBook(bookId);
return RedirectToAction("Books", "ProductManager");
}
#3
0
Your WebMethod must be static
您的WebMethod必须是静态的
[WebMethod(true)]
public static ActionResult DeleteBook(int bookId)
{
bookRepository.DeleteBook(bookId);
return RedirectToAction("Books", "ProductManager");
}