因为FF没回显的问题,让我郁闷了
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>AJAX Form</title>
- <script type="text/javascript">
- window.onerror=function(errorMessage,errorUrl,errorNum)
- {
- alert(errorMessage+errorUrl+errorNum);
- }
- var xmlHttp;
- function createXmlHttp()
- {
- if (window.ActiveXObject) { // IE浏览器
- try {
- xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try {
- xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (e) {}
- }
- } else {
- xmlHttp=new XMLHttpRequest();
- xmlHttp.overrideMimeType("text/xml");
- }
- if (!xmlHttp) { // 异常,创建对象实例失败
- window.alert("can not create XMLHttpRequest object.");
- return false;
- }
- }
- function startRequest()
- {
- try
- {
- createXmlHttp();
- var url="ajax.php";
- var postedData=getRequestBody(document.forms["form1"]);
- xmlHttp.open("post",url,true);
- xmlHttp.setRequestHeader("content-length",postedData.length);//post提交设置项
- xmlHttp.setRequestHeader("content-type","application/x-www-form-urlencoded");//post提交设置项
- xmlHttp.onreadystatechange =onComplete;
- //将名值对发送到服务器
- xmlHttp.send(postedData);
- }
- catch(e)
- {
- alert(e.message);
- }
- }
- function onComplete()
- {
- if(xmlHttp.readyState==4&&xmlHttp.status==200)
- {
- //显示结果
- // document.getElementById("divResult").innerText=xmlHttp.responseText;
- if (window.ActiveXObject)
- document.getElementById("divResult").innerText=xmlHttp.responseText;
- else
- document.getElementById("divResult").textContent=xmlHttp.responseText; //如果没这个 FF就没回显,
- }
- }
- //获取表单中的名值对
- function getRequestBody(oForm)
- {
- var aParams=new Array();
- for(var i=0;i<oForm.elements.length;i++)
- {
- var sParam=encodeURIComponent(oForm.elements[i].id)
- sParam+="=";
- sParam+=encodeURIComponent(oForm.elements[i].value);
- aParams.push(sParam);
- }
- return aParams.join("&");
- }
- </script>
- </head>
- <body>
- <form id="form1">
- Company:<input name="company" id="company" type="text" />
- TEL: <input name="tel" id="tel" type="text" />
- <div id="divResult"></div>
- </form>
- </body>
- </html>