Sonia
Sonia

Reputation: 253

XMLHttpRequest returns always undefined in IE9

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;

Upvotes: 4

Views: 2344

Answers (1)

andyb
andyb

Reputation: 43823

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.

Upvotes: 5

Related Questions