postdata.js

来自「PHP+AJAX留言本,站长爱好者留言系统!」· JavaScript 代码 · 共 77 行

JS
77
字号
var xmlHttp=createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
try
{
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
var XmlHttpVersions=new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
for(var i=0;i<XmlHttpVersions.length && !xmlHttp;i++)
{
 try
 {
  xmlHttp=new ActiveXObject(XmlHttpVersions[i]); 
 }
 catch(e){}
}
}
if(!xmlHttp)
 alert("Error creating the XMLHttpRequest object.");
else
 return xmlHttp;
}

function process()
{
if(xmlHttp)
{
  try
{
 username=encodeURIComponent(document.getElementById("author").value);
 content=encodeURIComponent(document.getElementById("content").value);
 xmlHttp.open("POST","postdata.php?author="+username+"&content="+content,true);
 xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
 xmlHttp.onreadystatechange=handleRequestStateChange;
 xmlHttp.send(null);
}catch(e)
{
  alert("Can't connect to server:\n"+e.toString());
}
}
}

function handleRequestStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
try
{
	//document.write "Hello,world!";
handleServerResponse();
//document.write("Hello,World!");
}
catch(e)
{
 alert("Error reading the response:" +e.toString());
 }
}
else
{
 alert("There was a problem retrieving the data:\n"+xmlHttp.statusText);
}
}
}

function handleServerResponse()
{
	
  myUser=document.getElementById('list_user');
  myContent=document.getElementById('list_content');
  myUser.innerHTML=username;
  myContent.innerHTML=content;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?