Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery ajax() option - xhr

Tags:

In jQuery ajax function there is xhr option. Does someone know more details, usability or sample usage of this option?

like image 329
Vaclav Kohout Avatar asked Oct 29 '09 15:10

Vaclav Kohout


People also ask

What is XHR in jQuery AJAX?

XMLHttpRequest (XHR) objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. XMLHttpRequest is used heavily in AJAX programming.

Is XHR same as Ajax?

XHR is the XMLHttpRequest Object which interacts with the server. Ajax technique in the nutshell leverages the XHR request to send and receive data from the webserver. This object is provided by the browser's javascript environment. It transfers the data between the web browser and server.

What is the major difference between jQuery Ajax and XMLHttpRequest XHR )?

XMLHttpRequest is the raw browser object that jQuery wraps into a more usable and simplified form and cross browser consistent functionality. jQuery. ajax is a general Ajax requester in jQuery that can do any type and content requests.

What are Ajax call options?

options − A set of key/value pairs that configure the Ajax request.


1 Answers

For this function, you want to return an appropriate XHR object for your browser. The default behavior is to use XMLHTTPRequest or the IE equivalent. Here's the default behavior:

jQuery.ajaxSettings.xhr = window.ActiveXObject ?
/* Microsoft failed to properly
 * implement the XMLHttpRequest in IE7 (can't request local files),
 * so we use the ActiveXObject when it is available
 * Additionally XMLHttpRequest can be disabled in IE7/IE8 so
 * we need a fallback.
 */
function() {
    return !this.isLocal && createStandardXHR() || createActiveXHR();
} :
// For all other browsers, use the standard XMLHttpRequest object
createStandardXHR;

Those two create methods createStandardXHR and createActiveXHR essentially call the basic XHR creation methods we've all known and loved for years. Here's createStandardXHR:

function createStandardXHR() {
try {
    return new window.XMLHttpRequest();
} catch( e ) {}
}

So, if you wanted to override this, you can simply pass in your own function that returns a new XMLHttpRequest() object.

Why would you want to do this? Let's say you need to make a cross domain HTTP request and are using an IFRAME shim to make it work using document.domain to work within the same origin rules. This is a good way to make your javascript load the XHR object from the correct frame based on what domain you want to talk to.

Twitter.com uses this technique.

JavaScript runs on http://twitter.com/ but the data is at http://api.twitter.com. They create an IFRAME pointing at api.twitter.com that simply sets document.domain to "twitter.com". They set document.domain to "twitter.com" in the main page, too.

Then, their JS, when making HTTP requests, just creates it from the IFRAME instead of the main page. Gets them through the same-origin policy.

You can do this with the xhr option to $.ajax(). Here's a snippet (imagine this code running on a page at http://myapp.com):

$.ajax({url: "http://api.myapp.com", xhr: function(){
  return new ($('#my_api_iframe')[0].contentWindow.XMLHttpRequest)();
}, success: function(html) {
    // format and output result
   }
});

That will work as long as both the main page and the iframe set their document.domain to the same value. (This is a hackish example: it won't work in some IE versions because I cheated and only used the standard XMLHttpRequest object - you'll need to fix.)

Hope that helps.

(edited to add: this is a technique necessary for older browsers - CORS support in most modern browsers would make this unnecessary)

Sujal

like image 195
sujal Avatar answered Sep 22 '22 16:09

sujal