现在的位置: 首页 > 综合 > 正文

Ajax同步和异步传输

2013年08月04日 ⁄ 综合 ⁄ 共 1644字 ⁄ 字号 评论关闭
Js代码 复制代码
  1. //同步传输模式   
  2.   
  3. function RequestByGet(nProducttemp,nCountrytemp)   
  4. {   
  5.     var xmlhttp   
  6.   
  7.     if (window.XMLHttpRequest)     
  8.     {     
  9.          //isIE   =   false;     
  10.          xmlhttp   =   new   XMLHttpRequest();     
  11.     }     
  12.     else if (window.ActiveXObject)   
  13.     {     
  14.          //isIE   =   true;     
  15.          xmlhttp   =   new   ActiveXObject("Microsoft.XMLHTTP");     
  16.     }   
  17.                     
  18.     //Web page location.   
  19.     var URL="http://www.baidu.com/;   
  20.     xmlhttp.open("GET",URL, false);   
  21.     //xmlhttp.SetRequestHeader("Content-Type","text/html; charset=Shift_JIS")   
  22.     xmlhttp.send(null);   
  23.     var result = xmlhttp.status;   
  24.       
  25.     //OK   
  26.     if(result==200)   
  27.     {   
  28.         document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;   
  29.     }   
  30.     xmlhttp = null;   
  31. }   
  32.   
  33.   
  34. //异步传输模式   
  35. var xmlhttp   
  36.   
  37. function RequestByGet(nProducttemp,nCountrytemp)   
  38. {   
  39.     if (window.XMLHttpRequest)     
  40.     {     
  41.          //isIE   =   false;     
  42.          xmlhttp   =   new   XMLHttpRequest();     
  43.     }     
  44.     else if (window.ActiveXObject)   
  45.     {     
  46.          //isIE   =   true;     
  47.          xmlhttp   =   new   ActiveXObject("Microsoft.XMLHTTP");     
  48.     }   
  49.                     
  50.     //Web page location.   
  51.     var URL="http://www.baidu.com/";   
  52.     xmlhttp.open("GET",URL, true);   
  53.     xmlhttp.onreadystatechange = handleResponse;   
  54.     //xmlhttp.SetRequestHeader("Content-Type","text/html; charset=UTF-8")   
  55.     xmlhttp.send(null);     
  56. }   
  57.   
  58. function handleResponse()   
  59. {   
  60.     if(xmlhttp.readyState == 4 && xmlhttp.status==200)   
  61.     {   
  62.         document.getElementById("div_RightBarBody").innerHTML=xmlhttp.responseText;   
  63.         xmlhttp = null;   
  64.     }  

抱歉!评论已关闭.