admin管理员组文章数量:1291089
How to know when all ajax calls are plete?,now I just use a delay timeout to wait the request plete,these is a better way?i want make sure ajax requests are pleted...
How to know when all ajax calls are plete?,now I just use a delay timeout to wait the request plete,these is a better way?i want make sure ajax requests are pleted...
Share Improve this question edited May 7, 2012 at 9:32 gdoron 150k59 gold badges302 silver badges371 bronze badges asked May 7, 2012 at 9:25 Zhen ZhangzhenZhen Zhangzhen 411 gold badge2 silver badges6 bronze badges 3- 3 Are you using a framework/library to make your Ajax requests? Like jQuery or prototype or anything similair? – w00 Commented May 7, 2012 at 9:27
- You should have included the code that send the ajax request... – gdoron Commented May 7, 2012 at 9:34
- In your title you ask how to know when one Ajax request is pleted, in the body you ask about all Ajax calls. That's a difference, what do you want to know? – Felix Kling Commented May 7, 2012 at 9:39
6 Answers
Reset to default 4The XMLHttpRequest
object has readyState
property and onreadystatechange
that you can manipulate like that:
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) { // xmlhttp.status==200 == successful request
// What you want here
}
}
xmlhttp.open("GET", "foo_url.php", true);
xmlhttp.send();
}
You can do it simpler with jQuery, if you're using that library:
$.ajax({
url: 'foo_url.php',
plete: function() {
// What you want here
}
});
If you're using jQuery's AJAX, it has a plete option, see docs
$.ajax({
url: 'foo.php',
plete: function(jqXHR, textStatus) {
alert('AJAX call plete');
}
});
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
// This is when your Ajax request is plete
}
}
that is depend on your library; ex, if you are using jquery then you can use success:
parameter and if javascript then you can use if (xmlhttp.readyState==4 && xmlhttp.status==200)
statement.
If you are using xmlhttp object for ajax call then when xmlhttp.readyState==4 then it is consider as ajax request is pleted.
$.ajax({
url: 'abc.php',
data: "&id=" + id ;
plete: function(data) {
alert(data.responseText);
}
});
本文标签: javascriptHow do I know when an ajax request is completedStack Overflow
版权声明:本文标题:javascript - How do I know when an ajax request is completed - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741506736a2382366.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论