2011-01-13 33 views
1

现在,我在加载网页的时候出现了ajax调用的问题,在body的onload事件中,我将它指定为调用startCount()updateTable()这两个函数包含使用ajax调用从服务器端的数据库获取数据的代码。问题是,当ajax返回它将只返回一个呼叫,另一个呼叫不响应。请帮助我发生什么,以及如何解决它。同时使用ajax调用的问题

这是身体

<body onLoad="setAjaxConnection();startCount();updateTable()"> 

我使用XMLHttpRequest与正常的JavaScript,我不使用jQuery在onload ....

+0

我在身体的onload是=====>的onLoad = “setAjaxConnection(); startCount(); updateTable()” – PlodPla 2011-01-13 17:44:19

+0

你能添加的代码三个功能呢? – 2011-01-13 17:44:25

+0

有什么特别的理由不使用jQuery? – 2011-01-13 17:44:47

回答

0

使用JavaScript关闭。此链接可以帮助

http://dev.fyicenter.com/Interview-Questions/AJAX/How_do_I_handle_concurrent_AJAX_requests_.html

function AJAXInteraction(url, callback) { 
    var req = init(); 
    req.onreadystatechange = processRequest; 

    function init() { 
     if (window.XMLHttpRequest) { 
     return new XMLHttpRequest(); 
     } else if (window.ActiveXObject) { 
     return new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    } 

    function processRequest() { 
     if (req.readyState == 4) { 
     if (req.status == 200) { 
      if (callback) callback(req.responseXML); 
     } 
     } 
    } 

    this.doGet = function() { 
     req.open("GET", url, true); 
     req.send(null); 
    } 

    this.doPost = function(body) { 
     req.open("POST", url, true); 
     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
     req.send(body); 
    } 
} 

function startCount() { 
    var ai = new AJAXInteraction("/path/to/count.php", function(){alert("After startCount");}); 
    ai.doGet(); 
} 

function updateTable() { 
    var ai = new AJAXInteraction("/path/to/update.php", function(){alert("After updateTable");}); 
    ai.doGet(); 
} 
0

有一个呼叫“的onSuccess”启动接下来的阿贾克斯呼叫。所以你可以打电话startCount(),当你返回时,你可以关闭updateTable()

0
function setAjaxConnection(){ 
    //call Ajax here 
    setAjaxConnectionResponce; 
} 

function setAjaxConnectionResponce(){ 
    //on readystate==4 
    startCount(); 
} 

function startCount(){ 
    // code for count 
    updateTable(); 
} 

function updateTable(){ 
    // code for update 
} 

<body onLoad="setAjaxConnection();">