JQuery 如何找出 ajax 错误是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2209671/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:01:16  来源:igfitidea点击:

JQuery How to find out what the ajax error is?

jqueryajaxdebugging

提问by George Mauer

I have the following bit of code which I'm just trying out by running in firebug

我有以下代码,我只是通过在 firebug 中运行来尝试

$.ajax({
  type:"POST",
  url:"http://mpdomain/WebService.asmx/Operation",
  data: "{'parameter1': '44906'}", 
  contentType: "application/json;charset=utf-8",
  dataType: "json",
  success: function(data) { alert("succsess") },
  error: function(e, ts, et) { alert(ts) }
})

In theory it should work. However the error handler is triggered, and ts is simply set to "error". How do I get more detail about what's gone wrong?

理论上它应该有效。然而,错误处理程序被触发,并且 ts 被简单地设置为“错误”。我如何获得有关出错的更多详细信息?

回答by ozsenegal

To see the error message from an AJAXrequest, you can do something like this:

要查看来自AJAX请求的错误消息,您可以执行以下操作:

$.ajax({
  type:"POST",
  url:"http://mpdomain/WebService.asmx/Operation",
  data: "{'parameter1': '44906'}", 
  contentType: "application/json;charset=utf-8",
  dataType: "json",
  success: function(data) { alert("success") },
  error: function(ts) { alert(ts.responseText) } // or console.log(ts.responseText)
});

Note that, inside the error function, you get the responseText.

请注意,在错误函数中,您将获得responseText.

回答by jhchen

The error message jQuery gives you is not very descriptive. It can either be "timeout", "error", "notmodified" or "parsererror." http://api.jquery.com/jQuery.ajax/so what you can conclude is that it's not a timeout, not modified or parse error that you are getting.

jQuery 给您的错误消息不是很具有描述性。它可以是“超时”、“错误”、“未修改”或“解析器错误”。http://api.jquery.com/jQuery.ajax/所以你可以得出的结论是这不是超时,不是你得到的修改或解析错误。

Make sure in Firebug you see the request set to the correct address and the correct data is being set. You can also view the response so if you also have access to the server code a quick and dirty way is just to echo what is going on server side and view the response with Firebug.

确保在 Firebug 中您看到请求设置为正确的地址并且正在设置正确的数据。您还可以查看响应,因此如果您还可以访问服务器代码,那么一种快速而肮脏的方法就是回显服务器端正在发生的事情并使用 Firebug 查看响应。

Also I'm not sure if this is an issue but try to set the data to {parameter1: 44906} (basically remove the quotes so you are passing in an object and not a string).

此外,我不确定这是否是一个问题,但尝试将数据设置为 {parameter1: 44906} (基本上删除引号,以便传入对象而不是字符串)。