jQuery的不能检索从本地主机的数据 [英] jQuery cannot retrieve data from localhost

查看:123
本文介绍了jQuery的不能检索从本地主机的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有jQuery的一个非常简单的一点找回我最新的tweet

I have a very simple bit of jQuery to retrieve my latest Tweet

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

这工作正常在我的桌面上的一个简单的HTML文件。然而,一旦文件从我的本地访问被返回(阿帕奇)任何数据。我在想,如果任何部分的Apache是​​阻塞的要求以某种方式?或者有什么其他的想法?

This works fine on a simple HTML file on my desktop. However, once the file is accessed from my localhost (apache) no data is being returned. I was wondering if any part of Apache was blocking the request somehow? Or any other ideas?

推荐答案

JavaScript的目前不能因的同源策略

JavaScript cannot currently make direct requests cross-domain due to the Same-origin Policy.

您是最好的选择可能寻找到 JSONP 这一点。

You're best bet is probably to look into JSONP for this.

您可以找到关于它的更多信息既 jQuery的

You can find more information on it from both jQuery:

如果该URL包含字符串回调=?在URL,该请求被代替视为JSONP。请参阅 JSONP 数据类型的讨论href="http://api.jquery.com/jQuery.ajax" rel="nofollow"> $阿贾克斯()了解更多详情。

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 $.ajax() for more details.

微博

参数:

      
  • 回调:可选。 只适用于JSON格式。如果提供,响应将使用JSONP格式,具有给定名称的回调。

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

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

希望这有助于。

修正...

如果 状态/ user_timeline 支持JSONP,它不记录这样。

If status/user_timeline supports JSONP, it's not documented as such.

您可能需要考虑建立一个跨域代理来得到你想要的数据。

You may have to look into setting up a Cross-Domain Proxy to get the data you want.

这篇关于jQuery的不能检索从本地主机的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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