我怎么能在POST时用AJAX发送信息呢


How could I send the information in AJAX at POST?

对不起我的英语。。。

如何使用POST发送Ajax中的信息?(infoinfo_1info_2

现在,我用GET 发送

edit:我试着按照这里的人说的去做,但当我在为他发送信息的页面中调用POST变量时,它会向我显示eror。。。为什么?

新代码:

var xhr = new XMLHttpRequest();
xhr.open("POST",url,true);
xhr.onreadystatechange = function() {
    if( this.readyState == 4 && this.status == 200) {
        document.getElementById(name).innerHTML = this.responseText;
    }
};
xhr.send("info="+str+"&info_1="+info_1+"&info_2="+info_2);
return false;

第一个代码:

var xhr = new XMLHttpRequest();
xhr.open("GET",url+"?info="+str+"&info_1="+info_1+"&info_2="+info_2,true);
xhr.onreadystatechange = function() {
    if( this.readyState == 4 && this.status == 200) {
        document.getElementById(name).innerHTML = this.responseText;
    }
};
xhr.send();
return false;

"GET"更改为"POST",并将数据(与查询字符串中的格式相同,但没有?前缀)作为send()方法的参数,而不是放在URL中。

我使用了这段代码。它工作

function getXMLHttpRequestObject()
{
  var xmlhttp;
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}
function callme1(){
var http = new getXMLHttpRequestObject();
var url = "yourpage.php";
var reply;
var conf=true;
var parameters = "info="+str+"&info_1="+info_1+"&info_2="+info_2;
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", parameters .length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function() {
    if(http.readyState == 4) {
        alert(http.responseText);   
    }
}
http.send(parameters);
}

只需调用函数callme1(),它就会将请求发布到yourpage.php

这是如何在ajax 中发送Post请求

var str="hrl";// initialize str
var info_1="hi"; // initialize info_1
var info_2="hi"; // initialize info_2
var url="index.jsp"; // initialize url
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST",url,true);// initialize url
xmlhttp.send("info="+ str +"&info_1="+ info_1 +"&info_2="+info_2+"");