jQuery json 未捕获语法错误:意外标记:

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7936610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 00:28:49  来源:igfitidea点击:

json Uncaught SyntaxError: Unexpected token :

jqueryajaxjsonsyntax-error

提问by paparush

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

尝试拨打电话并检索一个非常简单的单行 JSON 文件。

$(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

JSON 在响应中返回(红色:#f00),但 Chrome 报告Uncaught SyntaxError: Unexpected token : colours.json:1

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

如果我直接导​​航到 url 本身,则返回 JSON 并显示在浏览器中。

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

如果我将 colors.json 的内容粘贴到 JSLINT 中,则 json 会验证。

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.

编辑- 上面的 jQuery.ajax() 调用在 jsfiddle.net 上运行完美,并按预期返回警报“成功”。

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.

编辑 2- 这个 URL 工作正常 'http://api.wunderground.com/api/8ac447ee36aa2505/geolookup/conditions/q/IA/Cedar_Rapids.json' 我注意到它返回为 TYPE: text/javascript 并且 Chrome 没有抛出意外令牌。我已经测试了其他几个 url,唯一一个不抛出 Unexptected Token 的是 wunderground,它以 TYPE: text/javascript 的形式返回。

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

作为 text/plain 和 application/json 返回的流没有被正确解析。

回答by John Flatness

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

您已经告诉 jQuery 期待JSONP响应,这就是为什么 jQuery 将该callback=jQuery16406345664265099913_1319854793396&_=1319854793399部分添加到 URL 的原因(您可以在请求的转储中看到这一点)。

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

您返回的是 JSON,而不是 JSONP。你的回复看起来像

{"red" : "#f00"}

and jQuery is expecting something like this:

和 jQuery 期待这样的事情:

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

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

如果您确实需要使用 JSONP 来绕过同源策略,那么服务器服务colors.json需要能够实际返回 JSONP 响应。

If the same origin policy isn't an issue for your application, then you just need to fix the dataTypein your jQuery.ajaxcall to be jsoninstead of jsonp.

如果同源策略对您的应用程序来说不是问题,那么您只需dataType要将jQuery.ajax调用中的修复为,json而不是jsonp.

回答by S. Rehan

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/].

最近几天我一直在试图自己解决这个问题。使用旧的 json dataType 会给您带来跨源问题,而将 dataType 设置为 jsonp 会使数据“不可读”,如上所述。所以显然有两种方法,第一种对我不起作用,但似乎是一个潜在的解决方案,而且我可能做错了什么。这在这里解释 [ 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.

对我有用的方法如下: 1- 下载 [ http://www.ajax-cross-origin.com/] 中的 ajax cross origin 插件。2- 在普通 jQuery 链接的正下方添加一个脚本链接。3- 将“crossOrigin: true”行添加到您的 ajax 函数中。

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);
      }
    })

回答by user1510230

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

我遇到了同样的问题,解决方案是将 json 封装在此函数中

jsonp(

jsonp(

.... your json ...

.... 你的 json ...

)

)

回答by Keith.Abramo

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

该十六进制可能需要用引号括起来并制成字符串。Javascript 可能不喜欢 # 字符