建立AJax代碼記錄

<script type="text/javascript"> var xmlHttp; function createXMLHttpRequest(){ //識別瀏覽器類型  建立一個 xmlHttp對象 if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (ex) { try { xmlHttp = new ActiveXObject("Mirosoft.XMLHTTP"); } catch (ex) { } } } } //調用此方法 function sendMessage(){   if(xmlHttp == null){   createXMLHttpRequest();   }   var username = document.getElementById("username").value;     //指定請求的屬性   xmlHttp.open("POST","TestAjax?username="+username,true);   //指定回調函數   xmlHttp.onreadystatechange = callback;   //執行發送   xmlHttp.send(null);   }     //回調函數   function callback(){   //服務器返回成功   if(xmlHttp.status == 200 && xmlHttp.readyState == 4){   //服務器返回結果   var text = xmlHttp.responseText;     var mydiv=document.getElementById("mydiv"); mydiv.innerHTML=text;   }   } </script>
相關文章
相關標籤/搜索