AJAX向后台传参数

80酷酷网    80kuku.com

  ajax|后台 function createXMLHttpRequest(){
   if(window.ActiveXObject)
   {
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   else if(window.XMLHttpRequest)
   {
     xmlHttp = new XMLHttpRequest();
   }
  }
 
  function createQueryString()
  {
   var firstName = document.getElementById("firstName").value;
   var middleName = document.getElementById("middleName").value;
   var birthDay = document.getElementById("birthDay").value;
   var queryString = "firstName="+ firstName +"&middleName="+middleName+"&birthDay="+birthDay;
   return queryString;
  }
 
  function doRequestUsingGet(){
   createXMLHttpRequest();
  
   var queryString = "WebForm2.aspx?";
   queryString  = queryString + createQueryString() + "&timeStamp="+new Date().getTime();
   xmlHttp.onreadystatechange = handleStateChange;
   xmlHttp.open("GET",queryString,true);
   xmlHttp.send(null);
  }
 
  function doRequestUsingPost(){
   createXMLHttpRequest();
  
   var url   = "WebForm2.aspx?timeStamp="+new Date().getTime();
   var queryString = createQueryString();
   xmlHttp.open("POST",url,true);
   xmlHttp.onreadystatechange = handleStateChange;
   xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
   xmlHttp.send(queryString);
  }
 
  function handleStateChange(){
  
   if(xmlHttp.readyState == 4)
   {
    if(xmlHttp.status == 200)
    {
    
     parseResults();
    }
   }
  }
 
  function parseResults(){
   var responseDiv = document.getElementById("serverResponse");
   var testStr = xmlHttp.responseText;
   document.getElementById("firstName").value = testStr;
     }



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: