开发者

Problem of using ajax call at the same time

开发者 https://www.devze.com 2023-02-03 21:46 出处:网络
Now, I have a problem with the ajax call which is when the web page is loaded, in the onload event开发者_JAVA技巧 of body, I assign it to call the functions which are startCount() and updateTable()thi

Now, I have a problem with the ajax call which is when the web page is loaded, in the onload event开发者_JAVA技巧 of body, I assign it to call the functions which are startCount() and updateTable() this two functions contain the code that use ajax call to get the data from DB on the server side. The problem is when the ajax return it will return only one call and another call does not response. Please help me what happen and how I can slove it.

This is the onload in the body

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

I use the XMLHttpRequest with the normal javascript, I do not use jQuery....


Use javascript closures. This link may help

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();
}


Have the 'onSuccess' of one call initiate the next ajax call. So you would call startCount() and when that returns you fire off updateTable().


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();">
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号