Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json Uncaught SyntaxError: Unexpected token :

Trying to make a call and retrieve a very simple, one line, JSON file.

$(document).ready(function() {      jQuery.ajax({          type: 'GET',         url: 'http://wncrunners.com/admin/colors.json' ,         dataType: 'jsonp',          success: function(data) {              alert('success');         }     });     });//end document.ready 

Here's the RAW Request:

GET http://wncrunners.com/admin/colors.json?callback=jQuery16406345664265099913_1319854793396&_=1319854793399 HTTP/1.1 Host: wncrunners.com Connection: keep-alive Cache-Control: max-age=0 User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2 Accept: */* Referer: http://localhost:8888/jquery/Test.html Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 

Here's the RAW Response:

HTTP/1.1 200 OK Date: Sat, 29 Oct 2011 02:21:24 GMT Server: Apache/1.3.33 (Unix) mod_ssl/2.8.22 OpenSSL/0.9.7d SE/0.5.3 Last-Modified: Fri, 28 Oct 2011 17:48:47 GMT ETag: "166a2402-10-4eaaeaff" Accept-Ranges: bytes Content-Length: 16 Content-Type: text/plain Connection: close  {"red" : "#f00"} 

The JSON is coming back in the response (red : #f00), but Chrome reports Uncaught SyntaxError: Unexpected token : colors.json:1

If I navigate directly to url itself, the JSON is returned and is displayed in the browser.

If I paste the contents of colors.json into JSLINT, the json validates.

Any ideas why I can't get this error and I never make it to the success callback?

EDIT - the jQuery.ajax() call above runs perfect at jsfiddle.net, and returns the alert 'success' as expected.

EDIT 2 - this URL works fine 'http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json' I noticed that it returned as TYPE: text/javascript and Chrome did not throw the Unexpected Token. I've tested several other url's and the ONLY one that does not throw the Unexptected Token is the wunderground that is returned as TYPE: text/javascript.

Streams returned as text/plain and application/json are not being parsed correctly.

like image 300
paparush Avatar asked Oct 29 '11 02:10

paparush


People also ask

How do I fix unexpected token in JSON error?

The "Unexpected token u in JSON at position 0" error occurs when we pass an undefined value to the JSON. parse or $. parseJSON methods. To solve the error, inspect the value you're trying to parse and make sure it's a valid JSON string before parsing it.

What is uncaught SyntaxError unexpected token?

The "Uncaught SyntaxError: Unexpected token" occurs for multiple reasons: Having a <script /> tag that points to an HTML file instead of a JS file. Getting an HTML response from a server where JSON is expected. Having a <script /> tag that points to an incorrect path.

How do you handle JSON parsing error?

The "SyntaxError: JSON. parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.

What is unexpected token U in JSON at position 0?

The "Unexpected token u in JSON at position 0" is a typically Javascript error that will show up in your console log if the client has been directed to execute JSON. parse() on a string beginning with u instead of a stringified object. "u" is typically a stringified version of the undefined primitive.


1 Answers

You've told jQuery to expect a JSONP response, which is why jQuery has added the callback=jQuery16406345664265099913_1319854793396&_=1319854793399 part to the URL (you can see this in your dump of the request).

What you're returning is JSON, not JSONP. Your response looks like

{"red" : "#f00"} 

and jQuery is expecting something like this:

jQuery16406345664265099913_1319854793396({"red" : "#f00"}) 

If you actually need to use JSONP to get around the same origin policy, then the server serving colors.json needs to be able to actually return a JSONP response.

If the same origin policy isn't an issue for your application, then you just need to fix the dataType in your jQuery.ajax call to be json instead of jsonp.

like image 187
John Flatness Avatar answered Sep 23 '22 01:09

John Flatness