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

后端 未结 1 1172
孤街浪徒
孤街浪徒 2020-12-31 16:25

I need to addEventListener to listen for progress event before opening the XMLHttp connection (i.e. xhr.open()), but

相关标签:
1条回答
  • 2020-12-31 17:00

    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).

    0 讨论(0)
提交回复
热议问题