jQuery中的jqXHR对象

版权声明:wuyujin1997 reserve all rights. https://blog.csdn.net/wuyujin1997/article/details/89014576

intro

官网介绍

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.

As of jQuery 1.5.1, the jqXHR object also contains the overrideMimeType() method 
(it was available in jQuery 1.4.x, as well, but was temporarily removed in jQuery 1.5). 
jQuery .ajax()在jQuery 1.5中返回的jQuery XMLHttpRequest (jqXHR)对象是浏览器原生XMLHttpRequest对象的超集。
例如,它包含responseText和responseXML属性,以及getResponseHeader()方法。
当传输机制不是XMLHttpRequest(例如,JSONP请求的脚本标记)时,jqXHR对象在可能的情况下模拟本机XHR功能。
在jQuery 1.5.1中,jqXHR对象还包含overrideMimeType()方法(它在jQuery 1.4中可用)。x也是,但在jQuery 1.5中暂时删除了。

常用成员方法

jqxhr.done(function(data, textStatus, jqXHR) {}) success回调方法的替代。
jqxhr.fail(function(jqXHR, textStatus, errorThrown) {}) error回调的替代。
jqxhr.always(function(data|jqXHR, textStatus, jqXHR|errorThrown) {}) jQuery 1.6 added,是弃用的complete()方法的替代。如果方法成功,他就相当于done(),如果失败,相当于fail()
jqxhr.then(doneFn, failFn) jQuery 1.8 added,相当于done()fail()方法的合并。

NOTE
jQuery 3.0开始,jqxhr.success(), jqxhr.error(), jqxhr.complete将被移除。
推荐使用jqxhr.done(), jqxhr.fail(), jqxhr.always()

链式调用:

$.ajax(url, {
    /* settings */
}).done(function() {

}).fail(function() {

}).always(function() {

});

其他成员

为了向后兼容XMLHttpRequest对象,jqXHR对象也会显示以下的成员属性|方法:

readyState
responseXML
responseText
status
statusText

abort(?statusText)
getResponseHeader(name)
getAllResponseHeaders()
overrideMimeType(mimeType)
setRequestHeader(name, value)
statusCode(callbacksByStatusCode)

NOTE 没有onreadystatechange机制。不过done(), fail(), always()和statusCode()已经涵盖了所有可能的需求。

猜你喜欢

转载自blog.csdn.net/wuyujin1997/article/details/89014576