最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】js的jquery的ajax的get返回的error错误的详细信息

jQuery crifan 97浏览 0评论
折腾:
【已解决】用和适在线的语言合成接口把文字转语音
期间,考虑到jquery的ajax的get,此处如果出错,也需要把错误信息打印出来。
所以对于已有代码:
$.ajax({
  type : "GET",
  url : fullQaUrl,
  success: function(respJsonObj){
        console.log("respJsonObj=%o", respJsonObj);
  },
  error : function(err) {
        console.error("err=%s", err);
  }
});
需要去搞清楚,如何找到erro的详细信息
因为此处只能打印出error是Object:
main.js:224 err=Object
error @ main.js:224
fire @ jquery-1.11.1.js:3119
fireWith @ jquery-1.11.1.js:3231
done @ jquery-1.11.1.js:9277
callback @ jquery-1.11.1.js:9685
XMLHttpRequest.send (async)
send @ jquery-1.11.1.js:9631
ajax @ jquery-1.11.1.js:9176
ajaxSubmitInput @ main.js:74
(anonymous) @ main.js:55
dispatch @ jquery-1.11.1.js:4641
elemData.handle @ jquery-1.11.1.js:4309
无法得到error对象的具体错误信息
所以要去找找
jquery ajax  error
jQuery.get() | jQuery API Documentation
看文档:
.error() | jQuery API Documentation
jQuery.ajax() | jQuery API Documentation
解释是:
“* error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are “timeout”, “error”, “abort”, and “parsererror”. When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as “Not Found” or “Internal Server Error.”  As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn.  Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.”
好像是此处的error的第一个参数是jqXHR
去查了:
Types | jQuery API Documentation
看是具体是有哪些属性,好像不清楚
-》
jQuery.ajax() | jQuery API Documentation
“The jqXHR Object
The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser’s native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method. When the transport mechanism is something other than XMLHttpRequest (for example, a script tag for a JSONP request) the jqXHR object simulates native XHR functionality where possible.”
很复杂的样子,还是不知道全部的属性。
然后自己想到了:
console.log中,直接打印此处err这个object:
console.error(err);
是可以输出对象的信息的:
而此处的
status
statusText
responseText
才是我们要找的。
然后后来才注意到:
http://api.jquery.com/jQuery.ajax/#jqXHR
下面的解释中,有这些字段:
“For backward compatibility with XMLHttpRequest, a jqXHR object will expose the following properties and methods:
* readyState
* responseXML and/or responseText when the underlying request responded with xml and/or text, respectively
* status
* statusText
* abort( [ statusText ] )
* getAllResponseHeaders() as a string
* getResponseHeader( name )
* overrideMimeType( mimeType )
* setRequestHeader( name, value ) which departs from the standard by replacing the old value with the new one rather than concatenating the new value to the old one
* statusCode( callbacksByStatusCode )”
另外也才注意到:
jQuery.get() | jQuery API Documentation
中写了:
“The jqXHR.done() (for success), jqXHR.fail()(for error), and jqXHR.always() (for completion, whether success or error; added in jQuery 1.6)”
感觉是:
jquery 1.6之后,才加上error的函数的
jquery ajax error函数和及其参数详细说明-十有三博客
所以再去根据error有三个参数,再去优化
$.ajax({
  type : "GET",
  url : fullQaUrl,
  success: function(respJsonObj){
        console.log("respJsonObj=%o", respJsonObj);
  },
  error : function(jqXHR, textStatus, errorThrown) {
        console.error("jqXHR=%o, textStatus=%s, errorThrown=%s", jqXHR, textStatus, errorThrown);
        console.error(jqXHR);
  }
});
输出结果:
jqXHR={readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}, textStatus=error, errorThrown=NOT FOUND
【总结】
此处的jquery的ajax的error时,返回的信息是
error
Type: Function( jqXHR jqXHR, String textStatus, String errorThrown )
 其中jqXHR是个object,常见的(比如我此处只需要获取到error相关的描述)可能用到的属性有:
  • responseText
    • <!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 3.2 Final//EN”> <title>404 Not Found</title> <h1>Not Found</h1> <p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p> 
  • status:HTTP错误码
    • 404
  • statusText:HTTP错误描述
    • NOT FOUND
然后用代码:
$.ajax({
  type : "GET",
  url : fullQaUrl,
  success: function(respJsonObj){
        console.log("respJsonObj=%o", respJsonObj);
  },
  error : function(jqXHR, textStatus, errorThrown) {
        console.error("jqXHR=%o, textStatus=%s, errorThrown=%s", jqXHR, textStatus, errorThrown);
        console.error(jqXHR);

        var errDetail = String.format("status={0}\n\tstatusText={1}\n\tresponseText={2}", jqXHR.status, jqXHR.statusText, jqXHR.responseText);
        var errStr = String.format("GET: {0}\nERROR:\t{1}", fullQaUrl,  errDetail);
        // $('#response_text p').text(errStr);
        var responseError = $('#response_json').html('<pre><code class="html">' + errStr + "</code></pre>");
        console.log("responseError=%o", responseError);
        updateHighlight();
  }
});
即可输出错误详情

转载请注明:在路上 » 【已解决】js的jquery的ajax的get返回的error错误的详细信息

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
87 queries in 0.098 seconds, using 20.74MB memory