开发者

JSON.parse not working in Chrome [duplicate]

This question already has answers here: Invalid JSON in Chrome, no problem in Firefox (so strange!) (3 answers) Closed 7 years ago.

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);

    documen开发者_如何学Got.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;
}


Check out this question's answer, it has to do with the Unicode BOM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜