javascript Jquery getJSON Uncaught SyntaxError: Unexpected token : error
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6590608/
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
Jquery getJSON Uncaught SyntaxError: Unexpected token : error
提问by rogeliog
I am trying to connect to the RubyGems API, but when I try to get the JSON I get an stange error.
我正在尝试连接到RubyGems API,但是当我尝试获取 JSON 时,出现了一个奇怪的错误。
Uncaught SyntaxError: Unexpected token :
I have seen this error in other questions and posts, but none of the worked for me.
我在其他问题和帖子中看到过这个错误,但没有一个对我有用。
Here is my $.getJSON() function
这是我的 $.getJSON() 函数
.getJSON("http://rubygems.org/api/v1/gems/rails.json?jsoncallback=?", function(data) {
...function stuff....
});
This is the JSON that I should receive:
这是我应该收到的 JSON:
{"dependencies":{"runtime":[{"name":"actionmailer","requirements":"= 3.0.9"},{"name":"actionpack","requirements":"= 3.0.9"},{"name":"activerecord","requirements":"= 3.0.9"},{"name":"activeresource","requirements":"= 3.0.9"},{"name":"activesupport","requirements":"= 3.0.9"},{"name":"bundler","requirements":"~> 1.0"},{"name":"railties","requirements":"= 3.0.9"}],"development":[]},"name":"rails","downloads":4474748,"info":"Ruby on Rails is a full-stack web framework optimized for programmer happiness and sustainable productivity. It encourages beautiful code by favoring convention over configuration.","version_downloads":93309,"version":"3.0.9","homepage_uri":"http://www.rubyonrails.org","bug_tracker_uri":"http://rails.lighthouseapp.com/projects/8994-ruby-on-rails","source_code_uri":"http://github.com/rails/rails","gem_uri":"http://rubygems.org/gems/rails-3.0.9.gem","project_uri":"http://rubygems.org/gems/rails","authors":"David Heinemeier Hansson","mailing_list_uri":"http://groups.google.com/group/rubyonrails-talk","documentation_uri":"http://api.rubyonrails.org","wiki_uri":"http://wiki.rubyonrails.org"}
Can someone please help me :)
有人可以帮帮我吗 :)
回答by mu is too short
What makes you think that rubygems.org supports JSONP at all? I don't see any mention of JSONP in the documentationand when I do this:
是什么让您认为 rubygems.org 完全支持 JSONP?我在文档中没有看到任何提及 JSONP的内容,当我这样做时:
lynx -dump -source 'http://rubygems.org/api/v1/gems/rails.json?jsoncallback=x'
I get the same plain old JSON as I do from
我得到了和我一样的普通旧 JSON
lynx -dump -source 'http://rubygems.org/api/v1/gems/rails.json'
The only difference between the two is the downloads
and version_downloads
change but that's to be expected.
两者之间的唯一区别是downloads
和version_downloads
变化,但这是可以预料的。
When you use jsoncallback=?
in the query string, jQuery will set up a callback function and assume that the remote URL will send back JavaScript (not JSON!) that will call the specified function. So, if the remote service sends back JSON when you're expecting JavaScript, the browser will end up trying to interpret the JSON as JavaScript and get upset because
当您jsoncallback=?
在查询字符串中使用时,jQuery 将设置一个回调函数并假设远程 URL 将发回将调用指定函数的 JavaScript(不是 JSON!)。因此,如果远程服务在您期待 JavaScript 时发回 JSON,浏览器最终会尝试将 JSON 解释为 JavaScript 并感到不安,因为
{"dependencies":{"runtime":[{"name":"action ...
is not a valid JavaScript statement. This sounds exactly like the error you're seeing.
不是有效的 JavaScript 语句。这听起来与您看到的错误完全一样。
I think you're going to have to proxy the JSON through your own server. You'll need a controller on your server that makes that makes the call to get the JSON and then simply echoes it back to your JavaScript, this will get your around both the lack of JSONP support and your cross domain problem in your client.
我认为您将不得不通过自己的服务器代理 JSON。您的服务器上需要一个控制器来调用获取 JSON,然后简单地将其回显到您的 JavaScript,这将解决您的客户端缺乏 JSONP 支持和跨域问题。
回答by Cristian Sanchez
The browser is interpreting the curly brackets as a block. The API needs to return a function call with the JSON data as the argument (using the supplied callback name). It doesn't appear that this API supports JSONP & the callback parameter so you'll need to contact them to ask for this support.
浏览器将大括号解释为一个块。API 需要返回一个以 JSON 数据为参数的函数调用(使用提供的回调名称)。此 API 似乎不支持 JSONP 和回调参数,因此您需要与他们联系以寻求此支持。
It should return something like:
它应该返回如下内容:
jsonp1279196981233({ ... })
rather than simply:
而不是简单地:
{ ... }
because the client will be evaluating it as a script. In JavaScript a set of stand alone curly braces will denote a block.
因为客户端会将其作为脚本进行评估。在 JavaScript 中,一组独立的花括号将表示一个块。
回答by David Harkness
Given that you're passing in a data-handler function to getJSON()
, are you sure you want the JSONP callback parameter? Try removing ?jsoncallback=?
from the URL:
鉴于您将数据处理函数传递给getJSON()
,您确定需要 JSONP 回调参数吗?尝试?jsoncallback=?
从 URL 中删除:
.getJSON("http://rubygems.org/api/v1/gems/rails.json", function(data) {
...function stuff....
});
回答by lucygenik
Related... If you are not returning valid JSON you will see this error, JSONP aside.
相关...如果您没有返回有效的 JSON,您将看到此错误,JSONP 除外。
For me I was
对我来说我是
format.json { render :json => ActiveSupport::JSON.encode({:foo => true}) :status => :ok }
=>
{
'foo': true
}
When I should have been
当我应该
format.json { render :json => ActiveSupport::JSON.encode([{:foo => true}]) :status => :ok }
=>
[
{
'foo': true
}
]