当我正在执行Ajax POST请求时,我应该将什么参数放入发送中?

时间:2011-07-05 03:18:49

标签: javascript ajax

这是http://www.javascriptkit.com/dhtmltutors/ajaxgetpost2.shtml的代码,我对此有疑问。

var mypostrequest=new ajaxRequest()
mypostrequest.onreadystatechange=function(){
 if (mypostrequest.readyState==4){
  if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
   document.getElementById("result").innerHTML=mypostrequest.responseText
  }
  else{
   alert("An error has occured making the request")
  }
 }
}
var namevalue=encodeURIComponent(document.getElementById("name").value)
var agevalue=encodeURIComponent(document.getElementById("age").value)
var parameters="name="+namevalue+"&age="+agevalue
mypostrequest.open("POST", "basicform.php", true)
mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
mypostrequest.send(parameters)

在我的程序中,我想让我发布的内容来自表单。那么我应该在发送中制作参数是什么?另外,我不想用这个Ajax请求更改文档本身的任何内容,因此在代码的onreadystatechange部分,如果状态为4且状态为200而不是更改元素的innerHTML,我该怎么办? / p>

1 个答案:

答案 0 :(得分:0)

您应该输入网址编码数据,例如“name = Frank& last = Jones”

相关问题