ajax

 

  1. function validate(field) {
    1. var xmlHttp; 
    1. if(window.XMLHttpRequest) {  
    2.             xmlHttp = new XMLHttpRequest();  
    3.         } else if (window.ActiveXObject) {  
    4.             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
    5.         } 
  1.  
  2.  
  3.         //alert(document.getElementById("userId").value);  
  4.         //alert(field.value);  
  5.         if (trim(field.value).length != 0) {  
  6.             //創建Ajax核心對象XMLHttpRequest  
  7.             createXMLHttpRequest();  
  8.               
  9.             var url = "user_validate.jsp?userId=" + trim(field.value) + "&time=" + new Date().getTime();  
  10.               
  11.             //設置請求方式爲GET,設置請求的URL,設置爲異步提交  
  12.             xmlHttp.open("GET", url, true);  
  13.           
  14.             xmlHttp.onreadystatechange=function(){
  15.  
    1. if (xmlHttp.readyState == 4) {  
    2.             //HTTP協議狀態爲成功  
    3.             if (xmlHttp.status == 200) {  
    4.                 if (trim(xmlHttp.responseText) != "") {  
    5.                     //alert(xmlHttp.responseText);  
    6.                     document.getElementById("spanUserId").innerHTML = "<font color='red'>" + xmlHttp.responseText + "</font>" 
    7.                 }else {  
    8.                     document.getElementById("spanUserId").innerHTML = "";  
    9.                 }  
    10.             }else {  
    11.                 alert("請求失敗,錯誤碼=" + xmlHttp.status);  
    12.             }  
    13.         } 
  16. }  
  17.               
  18.             //將設置信息發送到Ajax引擎  
  19.             xmlHttp.send(null);  
  20.         } else {  
  21.             document.getElementById("spanUserId").innerHTML = "";  
  22.         }     
  23.     }  
  24.      

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章