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?

1
  • which is it? "index":0 or "index":"0"? Commented Jan 12, 2012 at 12:54

1 Answer 1

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.

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.