ajax模拟表单提交的方法
[ 2008-04-22 14:11:39 | 作者: admin ]
<form method="post" action="test.do" onsubmit="sendRequest();return false">
</form>
</form>
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);
}
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);
}
[最后修改由 admin, 于 2008-04-22 14:13:08]
评论Feed: http://blog.xg98.com/feed.asp?q=comment&id=1092
这篇日志没有评论。
此日志不可发表评论。