admin管理员组文章数量:1394764
I want to use the onreadystatechange
event from the (underlying) XMLHttpRequest
of JQuery's (2.0.2) $.ajax(...)
to fire synchronous ajax requests so I can show an accurate status indication to the end user for long running requests. But it seems this functionality has been removed from the latest version of JQuery (see here), making spinners with asynchronous web requests the only option for representing the activity to the user.
using XMLHttpRequest
I would do something like the following (though I still wan't to use JQuery), is there still a way in JQuery to gain access to a readystate change function? Is there perhaps a plugin that exposes the onreadystatechange
event?
function pad(width, string, padding) { // from stackoverflow/a/15660515/424963
return (width <= string.length) ? string : pad(width, string + padding, padding)
}
$(function(){
$("<div>Loading</div>").appendTo($("body"));
var anticache = "?anticache=" + new Date().getTime();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4){
$("div").html("Done");
}
else {
$("div").html("Loading" + pad(xhr.readyState, "", "."));
}
};
xhr.open("POST", "jsfiddle" + anticache, true );
xhr.send();
});
JSFiddle
I want to use the onreadystatechange
event from the (underlying) XMLHttpRequest
of JQuery's (2.0.2) $.ajax(...)
to fire synchronous ajax requests so I can show an accurate status indication to the end user for long running requests. But it seems this functionality has been removed from the latest version of JQuery (see here), making spinners with asynchronous web requests the only option for representing the activity to the user.
using XMLHttpRequest
I would do something like the following (though I still wan't to use JQuery), is there still a way in JQuery to gain access to a readystate change function? Is there perhaps a plugin that exposes the onreadystatechange
event?
function pad(width, string, padding) { // from stackoverflow./a/15660515/424963
return (width <= string.length) ? string : pad(width, string + padding, padding)
}
$(function(){
$("<div>Loading</div>").appendTo($("body"));
var anticache = "?anticache=" + new Date().getTime();
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4){
$("div").html("Done");
}
else {
$("div").html("Loading" + pad(xhr.readyState, "", "."));
}
};
xhr.open("POST", "jsfiddle" + anticache, true );
xhr.send();
});
JSFiddle
Share Improve this question edited May 23, 2017 at 11:45 CommunityBot 11 silver badge asked Sep 17, 2013 at 9:29 Dead.RabitDead.Rabit 1,9751 gold badge29 silver badges46 bronze badges1 Answer
Reset to default 7do you mean something like this:
var _orgAjax = jQuery.ajaxSettings.xhr;
jQuery.ajaxSettings.xhr = function () {
var xhr = _orgAjax();
xhr.onreadystatechange = function() {
//you can log xhr.readystate here
if( xhr.readyState == 4 ) {
$("div").html("Done");
}
}
return xhr;
};
$(function(){
$("<div>Loading</div>").appendTo($("body"));
var anticache = "?anticache=" + new Date().getTime();
$.ajax({
url: "http://fiddle.jshell",
error: function() {
console.log("error");
}
});
});
Demo:: jsFiddle
本文标签: javascriptonreadystatechange event with ajaxStack Overflow
版权声明:本文标题:javascript - onreadystatechange event with $.ajax - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744102210a2590917.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论