jQuery $.ajax extension XDomainRequest onprogress

2019-05-10 20:57发布

问题:

The short version:

I want to get this to work with this:

The long version:

I want to create a jQuery extension that adds a progress method to the $.ajax object and which works with IE8 & IE9's XDomainRequest object.

Currently, using the above plugins, I can only define progress event callback handlers for XMLHttpRequest objects.

However, XDomainRequest also provides an onprogress event. I basically need a wrapper for XDomainRequest. Eg. progressEvent.length would correspond to xdr.responseText.length.

I'd appreciate any suggestions on where to begin.

回答1:

Well, I worked this out. I ended up forking ajaxHooks which implements XDomainRequest via an ajax transporter.

I added support for an onprogress event callback named "progress" which can be defined with the original ajax object.

As per the W3C Standard, progressEvent.lengthComputable = false because we can't get the content length, and so progressEvent.total = 0;

See example below:

$(document).ready(function(){

    var download_url = YOUR_URL;

    $.ajax({

        url: download_url,
        cache: false,
        progress: function(jqXHR, progressEvent) {

            console.log(progressEvent.loaded);

        }
    })
});

See my ajaxHooks fork here.