user3441626
user3441626

Reputation: 1

invalid character in json response

in my cakephp controller i send the json response with

$response = array('success' => 1);
return json_encode($response);

i am alway getting a wrong json in my view: SyntaxError: JSON.parse: unexpected character i have tested with JSLint, the error is "unsafe Character" char 0 line 1 firebug console output returns 65279 for the following statement.

console.log(response.charCodeAt(0));

what can i do? is this an UTF-8 issue?

Upvotes: 0

Views: 1309

Answers (1)

display name
display name

Reputation: 4185

You may have the character  in your json string which is the Unicode Character ZERO WIDTH NO-BREAK SPACE (U+FEFF). It may be that you copied it into your code via a copy/paste without realizing it. It is not visible so it is hard to debug. Try copy the $response text into a text editor and erase the space.

Here is a post that may be related. https://stackoverflow.com/a/9691839/2777098

Upvotes: 1

Related Questions