一个AJAX的问题 长时间 无响应 慢

弄了半天靠了

同事指导下解决了

 

XML/HTML代码
  1. /*   
  2. var http_request=false;   
  3.   function send_request(url,method,postStr){//初始化,指定处理函数,发送请求的函数   
  4.     http_request=false;   
  5.     //开始初始化XMLHttpRequest对象   
  6.     if(window.XMLHttpRequest){//Mozilla浏览器   
  7.      http_request=new XMLHttpRequest();   
  8.      if(http_request.overrideMimeType){//设置MIME类别   
  9.        http_request.overrideMimeType("text/xml");   
  10.      }   
  11.     }   
  12.     else if(window.ActiveXObject){//IE浏览器   
  13.      try{   
  14.       http_request=new ActiveXObject("Msxml2.XMLHttp");   
  15.      }catch(e){   
  16.       try{   
  17.       http_request=new ActiveXobject("Microsoft.XMLHttp");   
  18.       }catch(e){}   
  19.      }   
  20.     }   
  21.     if(!http_request){//异常,创建对象实例失败   
  22.      window.alert("创建XMLHttp对象失败!");   
  23.      return false;   
  24.     }   
  25.     //确定发送请求方式,URL,及是否同步执行下段代码   
  26.     http_request.open(method,url,true);   
  27.     //不加下边这段POST不成功 具体做啥的不知道    
  28.     http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");     
  29.     if(method=="GET"){   
  30.     http_request.send(null);   
  31.     }else{   
  32.      http_request.send(postStr);   
  33.     }   
  34.   }   
  35.   */   
  36. var http_request;    
  37. function createXMLHttpRequest() {    
  38.   if (window.ActiveXObject) {    
  39.    http_request = new ActiveXObject("Microsoft.XMLHTTP");    
  40.   }else if (window.XMLHttpRequest) {    
  41.   http_request = new XMLHttpRequest();    
  42.   }    
  43. }    
  44. function send_request(url,method,postStr) {    
  45.    createXMLHttpRequest();    
  46.    //http_request.setRequestHeader("If-Modified-Since","0");   
  47.    http_request.open(method,url,true);   
  48.    http_request.setRequestHeader("If-Modified-Since","0");   
  49.     if(method=="GET"){   
  50.     http_request.send(null);   
  51.     }else{   
  52.      http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");        
  53.      http_request.send(postStr);   
  54.     }   
  55. }    
  56.   
  57.   
  58.   
  59.   //处理返回信息的函数   
  60.   function processrequest(){   
  61.     //alert(http_request.readyState);   
  62.     //alert(reobj);   
  63.    if(http_request.readyState==4){//判断对象状态   
  64.      if(http_request.status==200){//信息已成功返回,开始处理信息   
  65.       document.getElementById(reobj).innerHTML=http_request.responseText;   
  66.      }   
  67.      else{//页面不正常   
  68.       alert(http_request.readyState);   
  69.       alert("您所请求的页面不正常!");   
  70.      }   
  71.    }   
  72.   }   
  73.   function dopage(obj,url){   
  74.    document.getElementById(obj).innerHTML="正在读取数据...";   
  75.    //alert(url);   
  76.    send_request(url,"GET");   
  77.       reobj=obj;   
  78.     http_request.onreadystatechange=processrequest;   
  79.     //http_request.onreadystatechange=alert(http_request.readyState);   
  80.   
  81.    }   
  82.       
  83.  //下拉菜单 跳转选择静态第一页   
  84. function GoToWhere(s,p)     
  85. {   
  86.     var obj='result';   
  87.     var d=s.options[s.selectedIndex].value;    
  88.     surl="detail_list.php?status=2&pro_id="+p+"type="+d;   
  89.     document.getElementById(obj).innerHTML="正在读取数据...";   
  90.     send_request(surl,"GET");   
  91.     reobj=obj;   
  92.     //alert(http_request.readyState);   
  93.     http_request.onreadystatechange=processrequest;   
  94.     
  95. }      
  96.       
  97.    //post方法验证用户名与密码是否正确   
  98.    function getCheck(){   
  99.     var res =checkfrom();   
  100.         if(res){   
  101.                     var discuzuser=document.getElementById("discuzuser").value;   
  102.                     if(discuzuser){   
  103.                         document.form2.submit();       
  104.                     }   
  105.                     else{   
  106.                     var username=document.getElementById("username").value;   
  107.                     var password=document.getElementById("password").value;    
  108.                     var postStr = "username="+username+"password="+password;    
  109.                    
  110.                     //ajax post默认是UTF8的 去那边转码了   
  111.                     send_request("http://dp.cnmo.com/checkuser.php","POST",postStr);   
  112.                     http_request.onreadystatechange = function(){    
  113.                         if (http_request.readyState == 4) {   
  114.                                if (http_request.status == 200) {   
  115.                                 var http_result2 = http_request.responseText;   
  116.                                 if(http_result2){   
  117.                                 document.getElementById("checkuser").style.display="";   
  118.                                 document.getElementById("checkuser").innerHTML = http_result2;   
  119.                                 }else{   
  120.                                         document.form2.submit();              
  121.                                     }   
  122.                                 }   
  123.                             }   
  124.                         }   
  125.                     }   
  126.             }//end if res   
  127.  }     
  128.    

关键一句

 

XML/HTML代码
  1. http_request.setRequestHeader("If-Modified-Since","0");  

« 上一篇 | 下一篇 »

1条记录访客评论

我在   XMLHttpRequest   的open   方法之后,send   方法之前,使用  
  XMLHttp.setRequestHeader("If-Modified-Since",   "lastModified");  
  判断被读取的文件是否更新。  
    
  据我所知,设定了If-Modified-Since的头之后,如果服务器中被读取的文件没有更新,浏览器不会向服务器发送请求,文件会从浏览器的缓存中读取。  
    
  但是经过5秒钟读取一次的观察,服务器文件没有改变时:  
  IE基本没有刷新。原因到底是根本没有重新读取还是使用了缓存快得让我感觉不到,我不清楚。  
    
  FF有明显刷新的现象,晃动得很厉害。我在本地测试,不知道读取的是缓存还是重新发出了请求。  
    
  文件没有更新的情况下,能否让浏览器干脆不执行   open   和   send   方法?直接保持原样?  
  或者,为什么FF还是违令向服务器发出请求?  
    
  谢谢!

Post by 一粒沙 on 2008, May 27, 7:10 PM 引用此文发表评论 #1


发表评论

评论内容 (必填):