1

My Ajax-Request does return this result (which generally is a valid JSON-string):

{"status":"ERROR","message":"Errore: Ti prego di inserire la domanda!"}

This is the Response Header:

Connection:Keep-Alive
Content-Length:80
Content-Type:application/json; charset=utf-8
Date:Sat, 16 Feb 2013 10:02:18 GMT
Keep-Alive:timeout=5, max=97
Server:Apache/2.2.21 (Win32) PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By:PHP/5.3.8

When I directly copy/paste the JSON-string from the browser console to an JSON parser like http://jsonlint.com it results "Unexpected token". Why?

I've tested it also by rewriting the same string in a text editor - of course this one results "Valid"...

MonsJovis
  • 186
  • 1
  • 8

1 Answers1

2

As you say, what you've quoted is valid JSON. I expect you have one or more "invisible" characters in there causing the problem. (It/they aren't present in your question, copying and pasting that gives a "Valid" result.) That's usually what it is when we see this question.

This is borne out by the Context-Length header. It says the length of the content is 80, but there are only 71 characters in your JSON, none of which is a multibyte character in UTF-8. So I would tend to suspect invalid invisible characters, probably at the beginning or end of the response. You should be able to find them if you paste the same string that causes a problem on http://jsonlint.com into a basic text editor; usually they'll show up as funny symbols.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    Thank you a lot! You're right, there were invisible characters. A simple ob_end_clean() did solved it :) Thank you! – MonsJovis Feb 16 '13 at 10:37
  • The invisible character was the Byte Order Marker. I consider reading this: http://de.wikipedia.org/wiki/Byte_Order_Mark http://stackoverflow.com/questions/3589358/fix-incorrectly-displayed-encoding-on-an-html-document-with-php – MonsJovis Feb 16 '13 at 14:44