gpt4 book ai didi

javascript - Jquery getJSON 未捕获语法错误 : Unexpected token : error

转载 作者:行者123 更新时间:2023-11-30 07:10:51 26 4
gpt4 key购买 nike

我正在尝试连接到 RubyGems API ,但是当我尝试获取 JSON 时,出现了一个错误。

Uncaught SyntaxError: Unexpected token :

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

这是我的 $.getJSON() 函数

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

这是我应该收到的 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"}

谁能帮帮我:)

最佳答案

是什么让您认为 ruby​​gems.org 完全支持 JSONP?我在 the documentation 中没有看到任何关于 JSONP 的提及当我这样做时:

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

我从

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

两者之间的唯一区别是 downloadsversion_downloads 发生了变化,但这是意料之中的。

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

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

不是有效的 JavaScript 语句。这听起来与您看到的错误完全一样。

我认为您将不得不通过自己的服务器代理 JSON。你需要在你的服务器上有一个 Controller ,它可以调用以获取 JSON,然后简单地将它回显给你的 JavaScript,这将解决你的客户端缺乏 JSONP 支持和跨域问题。

关于javascript - Jquery getJSON 未捕获语法错误 : Unexpected token : error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6590608/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com