微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jQuery无法从localhost检索数据

我有一个非常简单的jQuery来检索我最新的推文

$.getJSON("http://twitter.com/statuses/user_timeline/username.json?count=1",function(data) {
              $("#tweet_text").html(data[0].text);
           });

这适用于我桌面上的简单HTML文件.但是,一旦从我的localhost(apache)访问该文件,就不会返回任何数据.我想知道Apache的任何部分是否以某种方式阻止了这个请求?还是其他任何想法?

解决方法

由于 Same-origin Policy,JavaScript目前无法跨域直接请求.

你最好的选择可能就是看看JSONP.

您可以从jQuery找到有关它的更多信息:

If the URL includes the string “callback=?” in the URL,the request is treated as JSONP instead. See the discussion of the jsonp data type in 07003 for more details.

Twitter

Parameters:

  • callback: Optional. Only available for JSON format. If supplied,the response will use the JSONP format with a callback of the given name.

    • Example: http://search.twitter.com/search.json?callback=foo&q=twitter

希望这可以帮助.

更正…

如果status/user_timeline支持JSONP,则不会记录.

您可能需要考虑设置Cross-Domain Proxy获取所需的数据.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐