ajax异步操作学习

创建xhr对象实现浏览器全兼容         

  function createXmlHttp() {//创建xhr对象          

       var xhobj = false;              

   try {               

      xhobj = new ActiveXObject("Msxml2.XMLHTTP"); // ie msxml3.0+      

           } catch (e) {               

      try {                 

        xhobj = new ActiveXObject("Microsoft.XMLHTTP"); //ie msxml2.6           

          }

catch (e2)

{           

              xhobj = false;          

           }            

     }              

   if (!xhobj && typeof XMLHttpRequest != 'undefined')

{

// Firefox, Opera 8.0+, Safari            

         xhobj = new XMLHttpRequest();         

        }            

     return xhobj;     

        }

通过GET方式进行Ajax操作; var xhr;       

  window.onload = function () {             xhr = createXmlHttp();         }         function doAjax() {             xhr.open("GET", "Ajax.aspx?isAjax=1", true);             xhr.onreadystatechange = watching;             xhr.send(null);         }         function watching() {             if (xhr.readyState==4) {                 if (xhr.status == 200) {                     var txt = xhr.responseText;                     document.getElementById("hh").innerHTML = txt;                 }             }             else {                 document.getElementById("hh").innerHTML="服务器出错了"+xhr.status;             }         }

posted on 2013-04-14 23:49  平小  阅读(115)  评论(0编辑  收藏  举报