-1

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
asked Aug 11, 2013 at 7:11
4
  • #Idoroni, POST is an HTTP method, not a variable, could you please clarify your question, may be with more code? Commented Aug 11, 2013 at 7:21
  • 1
    how do you access the POST-variables in send.php and what are the values of str, info_1 and info_2 Commented Aug 11, 2013 at 7:23
  • if you provide the full block of code, we'll be able to help you to find the issue, otherwise we can't guess the other code part ! Commented Aug 11, 2013 at 7:46
  • @CodyDmd the web dont let me write so long code. its tell me that i need write more explanation for the problem Commented Aug 11, 2013 at 7:55

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

5 Comments

where i need to put this?
Before you call xhr.send() ;)
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... :/
did you have any idea why?
I didn't have finish ;) Have a look now

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.