Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override jQuery's use of XMLHttpRequest in $.ajax?

Tags:

jquery

ajax

I need to addEventListener to listen for progress event before opening the XMLHttp connection (i.e. xhr.open()), but the beforeSend method returns an xhr instance already opened. How would I override it to add the listener properly?

(Note: You need to add the event listeners before calling open() on the request. Otherwise the progress events will not fire.)

like image 808
Kot Avatar asked Nov 29 '10 11:11

Kot


People also ask

Does ajax use XMLHttpRequest?

XMLHttpRequest is used heavily in AJAX programming. Despite its name, XMLHttpRequest can be used to retrieve any type of data, not just XML. If your communication needs to involve receiving event data or message data from a server, consider using server-sent events through the EventSource interface.

Is ajax and XMLHttpRequest same?

Ajax allows us to send and receive data from the webserver asynchronously without interfering with the current state or behavior of the web page or application. XHR is the XMLHttpRequest Object which interacts with the server.

Which method is used on the returned object of ajax () method if the ajax call fails?

If an AJAX request fails, you can react to the failure inside the callback function added via the fail() function of the object returned by the $. ajax() function. Here is a jQuery AJAX error handling example: var jqxhr = $.

How can I get specific data from ajax response?

You can't as it's asynchronous. If you want to do anything with it, you need to do it in a callback. How? Because it's asynchronous, javascript will fire off the ajax request, then immediately move on to execute the next bit of code, and will probably do so before the ajax response has been received.


1 Answers

You can override the xhr function in ajaxSetup or even on each individual $.ajax call. This is documented in the $.ajax docs (thanks Nick!).

Your code might look like this (untested) if you want to do this all the time:

(function() {
    var originalXhr = jQuery.ajaxSettings.xhr; 
    jQuery.ajaxSetup({
        xhr: function() {
            var req = originalXhr();
            if (req) {
                // Add your progress handler
            }
            return req;
        }
    });
})();

...or like this (live example) for just a particular request:

$.ajax({
  url: "path/to/resource",
  xhr: function() {
    var req = $.ajaxSettings.xhr();
    if (req) {
      // Add your handler here
    }
    return req;
  }
});

Unfortunately, although overriding xhr is documented, the location of the currently-configured one (jQuery.ajaxSettings.xhr) doesn't seem to be, so technically you're still relying on an undocumented feature by using jQuery.ajaxSettings.xhr in your code. You just need to double-check that that's still there on each dot release, it's probably not going to move around too much (and jQuery.ajaxSettings is at least mentioned in the docs, here).

like image 67
T.J. Crowder Avatar answered Nov 15 '22 21:11

T.J. Crowder