ldoroni
ldoroni

Reputation: 103

How could I send the information in AJAX at POST?

Sorry for my english...

How could I send the information that's in Ajax with POST? (info, info_1, info_2)

Now, I'm sending it with GET

edit: i try to do what people here said me to do, but when i call the POST variable in the page that i send for him the info, its show me eror... why?

the new code:

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;

the first code:

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;

Upvotes: 0

Views: 134

Answers (3)

SarathSprakash
SarathSprakash

Reputation: 4624

This is how to send Post request in ajax

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+"");

Upvotes: 0

Gaurav Sharma
Gaurav Sharma

Reputation: 755

I used this code. It worked

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);
}

Just call function callme1() and it will post the request to yourpage.php

Upvotes: 1

Quentin
Quentin

Reputation: 943207

Change "GET" to "POST" and put the data (in the same form as in the query string, but without the ? prefix) as the argument to the send() method instead of in the URL.

Upvotes: 1

Related Questions