4

i would want to know for what reason this code works on firefox, chrome and IE10, but not in IE9

var ajaxReq = new XMLHttpRequest();
var params = "name="+$('#name').val()
var url = "register.php";

ajaxReq.open("POST", url, true);                               

ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    
ajaxReq.setRequestHeader("Content-length",params.length);
ajaxReq.setRequestHeader("Connection", "close"); 

ajaxReq.onreadystatechange = function(){
 if(ajaxReq.readyState == 4 && ajaxReq.status == 200) 
{alert(ajaxReq.response)} //<---this results undefined

 }

The code contained in php file itself doesn't matters because for do some proofs i rent it very minimal:

header('Content-Type: text/json');
echo 'response';
exit;
3
  • 1
    Instead of .response, it should be .responseText or .responseXML - see HTTP response Commented Jun 3, 2013 at 8:30
  • wow it's worked! But can i use responseText in case i will receive JSON encode objects? Commented Jun 3, 2013 at 8:44
  • Yes, as JSON is a plain text format. I added this as an answer with a longer description. Commented Jun 3, 2013 at 8:51

1 Answer 1

5

Instead of .response, it should be .responseText or .responseXML - see HTTP response. In your case, I assume that changing to alert(ajaxReq.responseText); will fix it.

response is not a property of the XMLHttpRequest object, which is why the JavaScript engine is throwing an undefined error.

From the aforementioned documentation:

responseText will contain the response of the server in plain text by a conforming user agent

so use responseText for everything in plain text apart from XML, which includes JSON, as that is a plain text format.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.