1

I used the PHP built-in function json_encode() to covert an object array to json string. Then I use AJAX method to get the xmlhttp.responseText. I use document.write(xmlhttp.responseText) to check the response string. For example, the string is as follow:

{"index":0,"marks":2}

But, when I use JSON.parse() to parse the JSON string, I get a syntax error. So, I copy this string to JSONLint to check whether it's valid, the result is no.

Parse error on line 1:
{    "index": 0
^
Expecting '{', '['

But when I input it by hand, it's valid. Why?

tomsawyer
  • 95
  • 1
  • 8

1 Answers1

1

You may have some zero-width whitespace character or similar in your responseText.

As a note, I highly recommend using console.log instead of document.write for debugging.

user123444555621
  • 148,182
  • 27
  • 114
  • 126