<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
<html> |
<head> |
<title>GETVS.POST</title> |
<script language="javascript"> |
varxmlHttp; |
functioncreateXMLHttpRequest(){ |
if(window.ActiveXObject) |
xmlHttp=newActiveXObject("Microsoft.XMLHttp"); |
elseif(window.XMLHttpRequest) |
xmlHttp=newXMLHttpRequest(); |
} |
functioncreateQueryString(){ |
varfirstName=document.getElementById("firstName").value; |
varbirthday=document.getElementById("birthday").value; |
varqueryString="firstName="+firstName+"&birthday="+birthday; |
returnencodeURI(encodeURI(queryString)); //两次编码解决中文乱码问题 |
} |
//GET模式 |
functiondoRequestUsingGET(){ |
createXMLHttpRequest(); |
varqueryString="9-3.aspx?"; |
queryString+=createQueryString()+"×tamp="+newDate().getTime(); |
xmlHttp.onreadystatechange=handleStateChange; |
xmlHttp.open("GET",queryString); |
xmlHttp.send(null); |
} |
//POST模式 |
functiondoRequestUsingPOST(){ |
createXMLHttpRequest(); |
varurl="9-3.aspx?timestamp="+newDate().getTime(); |
varqueryString=createQueryString(); |
xmlHttp.open("POST",url); |
xmlHttp.onreadystatechange=handleStateChange; |
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); |
xmlHttp.send(queryString); |
} |
functionhandleStateChange(){ |
if(xmlHttp.readyState==4&&xmlHttp.status==200){ |
varresponseDiv=document.getElementById("serverResponse"); |
responseDiv.innerHTML=decodeURI(xmlHttp.responseText); //解码 |
} |
} |
</script> |
</head> |
|
<body> |
<h2>输入姓名和生日</h2> |
<form> |
<input type="text" id="firstName"/><br> |
<input type="text" id="birthday"/> |
</form> |
<form> |
<input type="button" value="GET" onclick="doRequestUsingGET();"/><br> |
<input type="button" value="POST" onclick="doRequestUsingPOST();"/> |
</form> |
<div id="serverResponse"></div> |
</body> |
</html> |