json Uncaught SyntaxError: Unexpected token :

2019-01-01 01:28发布

问题:

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.

回答1:

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.



回答2:

I have spent the last few days trying to figure this out myself. Using the old json dataType gives you cross origin problems, while setting the dataType to jsonp makes the data \"unreadable\" as explained above. So there are apparently two ways out, the first hasn\'t worked for me but seems like a potential solution and that I might be doing something wrong. This is explained here [ https://learn.jquery.com/ajax/working-with-jsonp/ ].

The one that worked for me is as follows: 1- download the ajax cross origin plug in [ http://www.ajax-cross-origin.com/ ]. 2- add a script link to it just below the normal jQuery link. 3- add the line \"crossOrigin: true,\" to your ajax function.

Good to go! here is my working code for this:

  $.ajax({
      crossOrigin: true,
      url : \"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.86,151.195&radius=5000&type=ATM&keyword=ATM&key=MyKey\",
      type : \"GET\",
      success:function(data){
         console.log(data);
      }
    })



回答3:

I had the same problem and the solution was to encapsulate the json inside this function

jsonp(

.... your json ...

)



回答4:

That hex might need to be wrapped in quotes and made into a string. Javascript might not like the # character