lupingyu 发表于 2013-1-23 02:51:08

AJAX并发出现的问题,急

在实际中遇到ajax并发请求出现的消息串发的情况,请达人给予帮助。
1.在ajax并发情况下,发现有时出现post消息体发串了。
比如说,页面上起两个定时器,每隔2s触发两个ajax请求,分别调用后台的A.do,B.do,传的参数分别为a,b。都采用post方法。
例如
xhConn.connect(服务器+"A.do",,POST", "a=aaa",
         function(xh) {
            alert(xh.responseText);
   });
   xhConn.connect(服务器+"B.do",POST", "b=bbb",
         function(xh) {
            alert(xh.responseText);
   });
后台收到A.do,B.do请求后,都会分别提取参数a,b的值,并返回"true";

结果发现有时候参数b=bbb跑到a.do的体中,造成后台出现异常。如request.getParemeter("a")为null。

2.继续上面的例子,我在测试中发现有时候alert时,出现的什么也没有的情况。也就是xh.responseText="";初步怀疑是没有发到指定的服务器上,可是为什么会这样的。我在function(xh)中增加这种情况出现后,并不影响前后发消息。难道是消息丢失。
以上都我在实验室环境中测试时发现的。理论说网络丢包可忽略不计把

附xmlhttpRequest对象池代码:
function XHConn()
{
var xmlhttp, bComplete = false;
xmlhttp = XHRFactory.getInstance();
if (!xmlhttp){ return null;}

this.connect = function(sURL, sMethod, sVars, fnDone)
{
    if (!xmlhttp){ return false;}
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {

   
      if (sMethod == "GET")
      {
      xmlhttp.open("GET", sURL+"?"+sVars, true);
xmlhttp.setRequestHeader("If-Modified-Since", "0");

      sVars = "";
      }
      else
      {

      xmlhttp.open(sMethod, sURL, true);
      //xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
xmlhttp.setRequestHeader("If-Modified-Since", "0");

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

      }
   xmlhttp.onreadystatechange = function(){

      if (xmlhttp.readyState == 4 && !bComplete)
      {
          bComplete = true;
          if(fnDone) {fnDone(xmlhttp);}
          XHRFactory.release(xmlhttp);
      }};

   xmlhttp.send(sVars);

    }
    catch(z) { return false; }
    return true;
};
this.connect2 = function(sURL, sMethod, sVars, fnDone)
{
    if (!xmlhttp) {return false;}
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
      xmlhttp.open(sMethod, sURL+"?"+sVars, false);
      sVars = "";
      }
      else
      {

      xmlhttp.open(sMethod, sURL, false);
      xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
      xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

      }
      xmlhttp.onreadystatechange = function(){

      if (xmlhttp.readyState == 4 && !bComplete)
      {
          bComplete = true;
          if(fnDone) {fnDone(xmlhttp);}
          XHRFactory.release(xmlhttp);
      }};

      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
};
return this;
}

function createXHR() {

if (window.XMLHttpRequest) {
       return new XMLHttpRequest();
   } else if (window.ActiveXObject) {
          try{
    return new ActiveXObject('Msxml2.XMLHTTP.5.0');
}catch(e){
      try{
   return new ActiveXObject('Msxml2.XMLHTTP.4.0');
      }catch(e){
             try{
                  return new ActiveXObject('Msxml2.XMLHTTP');
}catch(e){
    return new ActiveXObject('Microsoft.XMLHTTP');
}
}
}
      
   }
    }


for (var i = 0; i < poolSize; i++) {
   stack.push(createXHR());
}

return ({
release:function(xhr){
   xhr.onreadystatechange = nullFunction;
   stack.push(xhr);
},
getInstance:function(){
   if (stack.length < 1) {
    return createXHR();
   } else {
    return stack.pop();
   }
},
toString:function(){
   return "stack size = " + stack.length;
}
});
})();
页: [1]
查看完整版本: AJAX并发出现的问题,急