简体   繁体   English

在同一页面中有多个ajax调用?

[英]more than one ajax call in the same page?

I have the following code 我有以下代码

function ajaxCall(action,parameters){

if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

}else{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

//xmlhttp.overrideMimeType('text/html');

xmlhttp.onreadystatechange=function(){

if (xmlhttp.readyState==4 && xmlhttp.status==200){

var rtrv_data=xmlhttp.responseText;

alert(rtrv_data);



}

}
parameters='action=' + action + '&' + parameters;

xmlhttp.open("POST","ajax_calls.php" ,true);

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.setRequestHeader("Content-length", parameters.length);

xmlhttp.setRequestHeader("Connection", "close");

xmlhttp.send(parameters);

}

Assuming that i have this function called by some timer , and a click on the page called the function again , i only get one output ! 假设我有一个定时器调用此函数,并再次点击该页面上的函数,我只得到一个输出! one response ! 一个回应! hwo can i get the 2 ? 我能得到2吗?

thanky you . 谢谢你 。

The variable "xmlhttp" is a global (you used no "var") so this will never allow two simultaneous ajax calls because when the second call starts you will overwrite the same variable and that's the variable that's used inside the callback to retrieve the data. 变量“xmlhttp”是全局的(你没有使用“var”)所以这永远不会允许两个同时的ajax调用,因为当第二个调用开始时你将覆盖同一个变量,这是在回调中用来检索数据的变量。

You need to create a new variable each time to store the xml request object and also you need to use a closure for your completion callback... something like 您需要每次创建一个新变量来存储xml请求对象,并且还需要使用闭包来完成回调...类似于

var xmltthp = ... // this is a local variable

xmlhttp.onReadyStateChange = function() {
    // here you can use xmlhttp even if it's a local
    // it will be a different variable for each ajax request
}

Thanks! 谢谢! That worked for me! 这对我有用! Did this: 做过这个:

function refreshpage(){
    var mycode=document.getElementById("bcode").value;
        if (window.XMLHttpRequest){
      xmlhttpf=new XMLHttpRequest();
    }else{
    xmlhttpf=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttpf.onreadystatechange=function(){
        if (xmlhttpf.readyState==4 && xmlhttpf.status==200){
            document.getElementById("zones").innerHTML=xmlhttpf.responseText;
        }
    }
    xmlhttpf.open("GET","ajaxaki.php?code="+mycode,true);
    xmlhttpf.send();
}


function refresh_vehs(){
    var asma=document.getElementById("newasma").value;
        if (window.XMLHttpRequest){
      xmlhttpf_vehs=new XMLHttpRequest();
    }else{
    xmlhttpf_vehs=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttpf_vehs.onreadystatechange=function(){
        if (xmlhttpf_vehs.readyState==4 && xmlhttpf_vehs.status==200){
            document.getElementById("vehicles").innerHTML=xmlhttpf_vehs.responseText;
        }
    }
    xmlhttpf_vehs.open("GET","AJAX_vehicle_cards.php?asma="+asma,true);
    xmlhttpf_vehs.send();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM