Last Updated: February 25, 2016
·
5.505K
· iheartweb

Accessing the XHR object in jQuery's $.ajax

Its useful and somtimes necessary to access the native XHR when using $.ajax. When your in this spot - and still want jquery to do the heavy lifting, do this:

var xhr = $.ajaxSettings.xhr();

//Do what you want with the XHR Object. For Example:

xhr.upload.addEventListener( function( event ) {
//CODE!!!
}, false );

//Store the XHR in a closure.
function xhrProvider() {
return xhr;
}

//Then when using $.ajax specifiy a custom xhr like this:
$.ajax( {
url: 'http://w00wz3rs.com',
xhr: xhrProvider
} );