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()
已经涵盖了所有可能的需求。