Sorry for my english...
why this ajax code dosen't work properly ?
when I call the POST variable in the page that i send for him the info, its show me that the POST variable is not exist...
Can anybody explain why it is happening ?
part of the ajax 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;
prime
15.8k16 gold badges105 silver badges137 bronze badges
1 Answer 1
Some headers must be included for POST request:
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
xhr.setRequestHeader("Connection", "close");
Edit :
My script use a variable called 'params'. So add a var at the beginning of your script and put in it the content of your request :
var params = "info="+str+"&info_1="+info_1+"&info_2="+info_2;
And replace in the call of xhr.send "info="+str+"&info_1="+info_1+"&info_2="+info_2; by params:
xhr.send(params);
Here is the final script:
var xhr = new XMLHttpRequest();
var params = "info="+str+"&info_1="+info_1+"&info_2="+info_2;
xhr.open("POST",url,true);
xhr.onreadystatechange = function() {
if( this.readyState == 4 && this.status == 200) {
document.getElementById(name).innerHTML = this.responseText;
}
};
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-length", params.length);
xhr.setRequestHeader("Connection", "close");
xhr.send(params);
return false;
answered Aug 11, 2013 at 7:27
Pyrech
5911 gold badge4 silver badges14 bronze badges
Sign up to request clarification or add additional context in comments.
5 Comments
ldoroni
where i need to put this?
Pyrech
Before you call xhr.send() ;)
ldoroni
thaks for trying help me! :) but now the code isnt work at all... befor that it was load the send.php page and show me that the POST not exist. now its not load it at all... :/
ldoroni
did you have any idea why?
Pyrech
I didn't have finish ;) Have a look now
default
str,info_1andinfo_2