I have a strange problem I can't figure out. I is too simple to go wrong. The problem is present in Chrome 12.0.742.91 but works fine in IE8 8.0.6001.19048.
This is my AJAX callback function and this
is the XMLHttpRequest instance.
// default AJAX callback handler
function Handler() {
if (this.readyState == 4 && this.status == 200) {
// alert(">>"+this.responseText+"<<");
var Response = this.responseText;
// Response = '{"Status":0,"Data":"My Data"}';
document.getElementById("debug").innerHTML = Response;
var Ajax = JSON.parse(Response);
document.getElementById("Stat1").innerHTML = Ajax.Status+"<br />"+Ajax.Data;
}
The AJAX server is sending {"Status":0,"Data":"Server Data"}
and this is displayed exactly the same in the debug
element; Server Data
.
header('Content-type: application/json');
print '{"Status":0,"Data":"Server Data"}';
The alert(...) is displaying >>{"Status":0,"Data":"Server Data"}<<
, no extra spaces in front or after JSON data.
But when parsed with JSON.parse() I get a javascript error: Illegal token.
However, if I hard code the same string (activates the remarked line) JSON.parse() works without error and displays the data (My Data
) in the stat1
element.
In IE8 the Server Data
version works flawless...
Have I overlooked something fundamental or is there a known issue with Chrome?
[EDIT] By request you will find the XMLHTTPRequest instance creation here:
// Create the XMLHttpRequest object
function GetHTTPRequestObject() {
var httpRequest = null;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
httpRequest = new XMLHttpRequest();
} else {// code for IE6, IE5
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
return httpRequest;
}