Ajax使用POST方法
来源:网络收集 责任编辑:栏目编辑 发表时间:2013-07-01 13:54 点击:次
Ajax使用POST方法
<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);
}
<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);
}
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>