$.ajax 实用程序中的 JQuery 错误选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/95600/
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 08:41:55  来源:igfitidea点击:

JQuery error option in $.ajax utility

jqueryajax

提问by Jay Corbett

The documentation indicates that the error: option function will make available: XHR instance, a status message string (in this case always error) and an optional exception object returned from the XHR instance (Book: JQuery in Action)

文档表明 error: option 函数将提供:XHR 实例、状态消息字符串(在这种情况下总是错误)和从 XHR 实例返回的可选异常对象(Book:JQuery in Action)

Using the following (in the $.ajax call) I was able to determine I had a "parsererror" and a "timeout" (since I added the timeout: option) error

使用以下(在 $.ajax 调用中)我能够确定我有一个“解析器错误”和一个“超时”(因为我添加了超时:选项)错误

error: function(request, error){}

What are other things you evaluate in the error option? do you include the optional exception object?

您在错误选项中评估的其他内容是什么?你包括可选的异常对象吗?

EDIT:one of the answers indicates all the return errors...learning more about what is of value (for debugging) in the XHR instance and exception object would be helpful

编辑:答案之一表明所有返回错误...了解有关 XHR 实例和异常对象中的价值(用于调试)的更多信息会有所帮助

This is a complete $.ajax call:

这是一个完整的 $.ajax 调用:

$.ajax({
 type: "post",
 url: "http://myServer/cgi-bin/broker" ,
 dataType: "text",
 data: {
 '_service' : 'myService',
 '_program' : 'myProgram',
 'start' : start,
 'end' : end
 },
 beforeSend: function() {
  $("#loading").removeClass("hide");
 },
 timeout: 5000,
 error: function(request,error) {
  $("#loading").addClass("hide");
  if (error == "timeout") {
   $("#error").append("The request timed out, please resubmit");
  }
  else {
   $("#error").append("ERROR: " + error);
  }
  },
  success: function(request) {
   $("#loading").addClass("hide");
   var t = eval( "(" + request + ")" ) ;
  } // End success
}); // End ajax method

Thanks for the input

感谢您的输入

采纳答案by Zach

Looking at the jQuery source code, there are four returned statuses, in additon to success:

查看 jQuery 源代码,除了成功之外,还有四种返回状态:

  • timeout- when your specified timeout is exceeded
  • error- http error, like 404
  • notmodified- when requested resource was not modified since last request
  • parsererror- when an xml/json response is bad
  • timeout- 超过您指定的超时时间
  • 错误- http 错误,如 404
  • notmodified-当请求的资源从去年的请求没有被修改
  • 解析器错误- 当 xml/json 响应不好时

回答by Matt

I find the request more useful than the error.

我发现请求比错误更有用。

error:function(xhr,err){
    alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
    alert("responseText: "+xhr.responseText);
}

xhris XmlHttpRequest.
readyStatevalues are 1:loading, 2:loaded, 3:interactive, 4:complete.
statusis the HTTP status number, i.e. 404: not found, 500: server error, 200: ok.
responseTextis the response from the server - this could be text or JSON from the web service, or HTML from the web server.

xhr是 XmlHttpRequest。
readyState值为 1:loading, 2:loaded, 3:interactive, 4:complete
status是 HTTP 状态号,即 404: not found, 500: server error, 200: ok
responseText是来自服务器的响应 - 这可以是来自 Web 服务的文本或 JSON,或者来自 Web 服务器的 HTML。

回答by davegurnell

This is an aside, but I think there's a bug in the code you submitted. The line:

这是旁白,但我认为您提交的代码中存在错误。线路:

 if (error = "timeout") {

should have more equals signs in it:

应该有更多的等号:

 if (error == "timeout") {

回答by Jataro

The second argument that is passed to your error function will either be the string "timeout" "parserror" "error" or "notmodified". The third will be the exception object. This object can be helpful for debugging.

传递给错误函数的第二个参数将是字符串“超时”“解析错误”“错误”或“未修改”。第三个将是异常对象。此对象有助于调试。

回答by Tomasz Tybulewicz

Are you sure that response is correct? Parse error mean that there is sth wrong with data being evaluted in line var t = eval( "(" + request + ")" ) ;

你确定这个回答是正确的吗?解析错误意味着在线评估数据存在错误 var t = eval( "(" + request + ")" ) ;