监视/侦听 Ajax readyState
Monitor / listen for Ajax readyState
请参阅下面的代码:
function createXMLHttpRequestObject() {
// will store the reference to the XMLHttpRequest object
var ajaxRequest;
// create the XMLHttpRequest object
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// return the created object or display an error message
if (!ajaxRequest) alert("Error creating the XMLHttpRequest object.");
else return ajaxRequest;
}
function ajax_update() {
var ajaxRequest = createXMLHttpRequestObject();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState != 4){
...
}
if(ajaxRequest.readyState == 4){
//process JSON data
}
}
}
我正在尝试从 ajax_update() 函数外部监视/侦听 ajaxRequest.readyState 值。ajax_update() 在按钮上触发。
我的目标是在函数 ajax_update() 之外,仅在所有 Ajax 调用完成时才触发另一个 JS 函数,这意味着 ajaxRequest.readyState==4。
例如:
<input type='button' value='SEND QUOTE' onclick='"ajax_update(some params); function_that_fires_when_readystate_is_completed();'">
有什么想法吗?
提前谢谢你!
使用回调。
.JS
function ajax_update(callback) {
var ajaxRequest = createXMLHttpRequestObject();
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
callback();
}
};
xmlhttp.open(...);
xmlhttp.send(null);
}
.HTML
<input onclick="ajax_update(function_that_listens_to_readyState);">
ajaxRequest1.onreadystatechange = function(){
if(ajaxRequest.readyState != 4){
...
}
if(ajaxRequest.readyState == 4){
//process JSON data
firstIsReady = true;
check();
}
}
ajaxRequest2.onreadystatechange = function(){
if(ajaxRequest.readyState != 4){
...
}
if(ajaxRequest.readyState == 4){
//process JSON data
secondIsReady = true;
check();
}
}
function check() {
if (firstIsReady && secondIsReady) {
//both ajax calls completed
}
}
类似的东西。这真的很丑,但不知道你想做什么,我不能给你更好的方法。
定义此全局性
var requestCounter = 0;
然后
function ajax_update() {
var ajaxRequest = createXMLHttpRequestObject();
//after calling request.open();
requestCounter++;
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState != 4){
requestCounter--;
//error code here
}
if(ajaxRequest.readyState == 4){
//process JSON data
requestCounter--;
}
if (requestCounter == 0)
onAllRequestComplete();
}
}
function onAllRequestComplete(){
// code that have to run when all requests have been completed
}
希望有帮助。
相关文章:
- readyState=4三次.Ajax-由警报引起
- 使用php的Ajax,获取状态200,但readyState为0
- 监视/侦听 Ajax readyState
- jquery ajax readystate 0 responsetext status 0 statustext er
- readyState event for HEADERS_RECEIVED using jQuery.ajax
- Ajax and readyState
- Ajax onreadystatechange函数,readystate=4和status=200被多次调用
- 初学者 AJAX 课程 - 为什么我必须将 request.readyState 包装在一个函数中
- iOS CORS ajax request readyState 0
- AJAX:在readyState=1时停止运行
- Ajax request -> request.readyState empty
- Jquery ajax调用返回一个错误,readystate 4, status 200, statustext ok
- Ajax readyState总是等于1
- 如果AJAX中的(xmlhttp.readyState==4&&xmlhttp.status==200)
- readyState的Ajax值总是大于4
- 带有公共参数的AJAX调用总是给出readyState = 1
- AJAX ReadyState 4状态200,但responseText未定义
- AJAX condition if (xmlhttp.readyState==4 && xmlhttp.
- 为什么可以'在执行ajax时无法获得readyState 3
- Ajax onreadystatechange function readyState