jq ajax xhr是什么,通过XHR的jQuery Ajax进度

我正在尝试捕获ajax请求的进度。

它没有按预期工作。据我所知,id为 progressProdCounter的 Div

应该在其中包含%的内容,但在我的情况下什么也没有发生。有帮助吗? __

在我看来,这if (evt.lengthComputable) {是行不通的XHR

HTML:

Loading Loading

JS:

var progressElem = $(#progressCounter);

var URL = "https://api.github.com/users/mralexgray/repos";

$("#loading").hide();

// write something in #progressCounter , later will be changed to percentage

progressElem.text(URL);

$.ajax({

type: GET,

dataType: json,

url: URL,

cache: false,

error: function (xhr, ajaxOptions, thrownError) {

alert(xhr.responseText);

alert(thrownError);

},

xhr: function () {

var xhr = new window.XMLHttpRequest();

//Download progress

xhr.addEventListener("progress", function (evt) {

if (evt.lengthComputable) {

var percentComplete = evt.loaded / evt.total;

progressElem.html(Math.round(percentComplete * 100) + "%");

}

}, false);

return xhr;

},

beforeSend: function () {

$(#loading).show();

},

complete: function () {

$("#loading").hide();

},

success: function (json) {

$("#data").html("data receieved");

}

});

我正在尝试捕获ajax请求的进度。 它没有按预期工作。据我所知,id为 progressProdCounter的 Div 应该在其中包含%的内容,但在我的情况下什么也没有发生。有帮助吗? __ 在我看来,这if (evt.lengthComputable) {是行不通的XHR HTML: Loading JS: var progressElem = $(#progressCounter); var URL = "https://api.github.com/users/mralexgray/repos"; $("#loading").hide(); // write something in #progressCounter , later will be changed to percentage progressElem.text(URL); $.ajax({ type: GET, dataType: json, url: URL, cache: false, error: function (xhr, ajaxOptions, thrownError) { alert(xhr.responseText); alert(thrownError); }, xhr: function () { var xhr = new window.XMLHttpRequest(); //Download progress xhr.addEventListener("progress", function (evt) { if (evt.lengthComputable) { var percentComplete = evt.loaded / evt.total; progressElem.html(Math.round(percentComplete * 100) + "%"); } }, false); return xhr; }, beforeSend: function () { $(#loading).show(); }, complete: function () { $("#loading").hide(); }, success: function (json) { $("#data").html("data receieved"); } });
经验分享 程序员 微信小程序 职场和发展