Last Updated: February 25, 2016
·
5.806K
· 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
} );

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside