Jquery getJSON Uncaught SyntaxError:意外的令牌:错误 [英] Jquery getJSON Uncaught SyntaxError: Unexpected token : error

查看:78
本文介绍了Jquery getJSON Uncaught SyntaxError:意外的令牌:错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接到 RubyGems API ,但当我尝试得到JSON我得到一个stange错误。

I am trying to connect to the RubyGems API, but when I try to get the JSON I get an stange error.

Uncaught SyntaxError: Unexpected token :

我在其他问题和帖子中看到过这个错误,但没有一个对我有用。

I have seen this error in other questions and posts, but none of the worked for me.

这是我的$ .getJSON()函数

Here is my $.getJSON() function

.getJSON("http://rubygems.org/api/v1/gems/rails.json?jsoncallback=?", function(data) {
  ...function stuff....
});

这是我应该收到的JSON:

This is the JSON that I should receive:

{"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"}

有人可以帮助我:)

推荐答案

是什么让你认为rubygems.org支持JSONP?我没有在文档中看到任何JSONP提及,当我这样做时:

What makes you think that rubygems.org supports JSONP at all? I don't see any mention of JSONP in the documentation and when I do this:

lynx -dump -source 'http://rubygems.org/api/v1/gems/rails.json?jsoncallback=x'

我得到了与我相同的普通旧JSON

I get the same plain old JSON as I do from

lynx -dump -source 'http://rubygems.org/api/v1/gems/rails.json'

两者之间的唯一区别是下载 version_downloads 改变,但这是预期的。

The only difference between the two is the downloads and version_downloads change but that's to be expected.

当你在查询字符串中使用 jsoncallback =?时,jQuery将设置一个回调函数,并假设远程URL将发回将调用指定函数的JavaScript(而不是JSON!)。因此,如果远程服务在您期待JavaScript时发送回JSON,浏览器将最终尝试将JSON解释为JavaScript并因此而感到不安,因为

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

{"dependencies":{"runtime":[{"name":"action ...

不是一个有效的JavaScript语句。这听起来就像你看到的错误。

is not a valid JavaScript statement. This sounds exactly like the error you're seeing.

我认为你将不得不代理JSON通过你自己的服务器。你需要一个控制器在你的服务器上调用来获取JSON,然后简单地回复你的JavaScript,这将使你无法获得JSONP支持和你的跨域你客户的问题。

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.

这篇关于Jquery getJSON Uncaught SyntaxError:意外的令牌:错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆