AJAX提交表單

因爲FF沒回顯的問題,讓我鬱悶了

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml" > 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
  5. <title>AJAX Form</title> 
  6.     <script type="text/javascript"> 
  7.     window.onerror=function(errorMessage,errorUrl,errorNum) 
  8.     { 
  9.         alert(errorMessage+errorUrl+errorNum); 
  10.     } 
  11.      var xmlHttp; 
  12.      function createXmlHttp() 
  13.      { 
  14.          if (window.ActiveXObject) { // IE瀏覽器 
  15.                  try { 
  16.                          xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); 
  17.                  } catch (e) { 
  18.                          try { 
  19.                                  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
  20.                          } catch (e) {} 
  21.                  } 
  22.          } else { 
  23.             xmlHttp=new XMLHttpRequest(); 
  24.             xmlHttp.overrideMimeType("text/xml"); 
  25.         } 
  26.          if (!xmlHttp) { // 異常,創建對象實例失敗 
  27.                  window.alert("can not create XMLHttpRequest object."); 
  28.                  return false; 
  29.          } 
  30.  
  31.  
  32.      } 
  33.      function startRequest() 
  34.      { 
  35.       try 
  36.       { 
  37.          createXmlHttp(); 
  38.          var url="ajax.php"
  39.          var postedData=getRequestBody(document.forms["form1"]); 
  40.         
  41.          xmlHttp.open("post",url,true); 
  42.          xmlHttp.setRequestHeader("content-length",postedData.length);//post提交設置項 
  43.          xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");//post提交設置項 
  44.          xmlHttp.onreadystatechange =onComplete
  45.          //將名值對發送到服務器 
  46.          xmlHttp.send(postedData); 
  47.        } 
  48.        catch(e) 
  49.        { 
  50.          alert(e.message); 
  51.        } 
  52.      } 
  53.      function onComplete() 
  54.      { 
  55.         if(xmlHttp.readyState==4&&xmlHttp.status==200) 
  56.          { 
  57.             //顯示結果 
  58.            // document.getElementById("divResult").innerText=xmlHttp.responseText; 
  59.  
  60.             if (window.ActiveXObject) 
  61.             document.getElementById("divResult").innerText=xmlHttp.responseText; 
  62.             else 
  63.             document.getElementById("divResult").textContent=xmlHttp.responseText;  //如果沒這個 FF就沒回顯, 
  64.          } 
  65.      } 
  66.      //獲取表單中的名值對 
  67.      function getRequestBody(oForm) 
  68.      { 
  69.         var aParams=new Array(); 
  70.         for(var i=0;i<oForm.elements.length;i++) 
  71.         { 
  72.           var sParam=encodeURIComponent(oForm.elements[i].id) 
  73.           sParam+="="; 
  74.           sParam+=encodeURIComponent(oForm.elements[i].value); 
  75.           aParams.push(sParam); 
  76.         } 
  77.         return aParams.join("&"); 
  78.      } 
  79. </script> 
  80. </head> 
  81. <body> 
  82. <form id="form1"> 
  83.  
  84.     Company:<input name="company" id="company" type="text" /> 
  85. TEL:     <input name="tel" id="tel" type="text" /> 
  86.  
  87.   <div id="divResult"></div> 
  88. </form> 
  89.  
  90. </body> 
  91. </html>  

 

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