| Ajax使用POST方法 |
| [ 作者:佚名 转贴自:网络转载 阅读次数:50 更新时间:2007-5-10 13:40:00 录入:刘光勇 ] 热 |
|
|
|
<form method="post" action="test.do" onsubmit="sendRequest();return false">
function getRequestBody(oForm){ var aParams=new Array(); for(var i=0;i<oForm.elements.length;i++){ var sParam=encodeURIComponent(oForm.elements[i].name); sParam+="="; sParam+=encodeURIComponent(oForm.elements[i].value); sParams.push(sParam); } return aParams.join("&"); }
function sendRequest(){ var oForm=document.forms[0]; var sBody=getRequestBody(oForm); var oXmlHttp=createRequest(); oXmlHttp.open("post",oForm.action,true); oXmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); oXmlHttp.onreadystatechange=function(){ if(oXmlHttp.readyState==4){ saveResult(oXmlHttp.responseText); }else{ saveResult("An error accurred: "+oXmlHttp.statusText); } } oXmlHttp.send(sBody); }
|
|
|