javascript – Uncaught SyntaxError: Unexpected token :
javascript – Uncaught SyntaxError: Unexpected token :
Seeing red errors
Uncaught SyntaxError: Unexpected token <
in your Chrome developers console tab is an indication of HTML in the response body.
What youre actually seeing is your browsers reaction to the unexpected top line <!DOCTYPE html>
from the server.
Just an FYI for people who might have the same problem — I just had to make my server send back the JSON as application/json and the default jQuery handler worked fine.
javascript – Uncaught SyntaxError: Unexpected token :
This has just happened to me, and the reason was none of the reasons above. I was using the jQuery command getJSON and adding callback=?
to use JSONP (as I needed to go cross-domain), and returning the JSON code {foo:bar}
and getting the error.
This is because I should have included the callback data, something like jQuery17209314005577471107_1335958194322({foo:bar})
Here is the PHP code I used to achieve this, which degrades if JSON (without a callback) is used:
$ret[foo] = bar;
finish();
function finish() {
header(content-type:application/json);
if ($_GET[callback]) {
print $_GET[callback].(;
}
print json_encode($GLOBALS[ret]);
if ($_GET[callback]) {
print );
}
exit;
}
Hopefully that will help someone in the future.