jQuery无法从localhost检索数据
I have a very simple bit of jQuery to retrieve my latest Tweet
我有一个非常简单的jQuery来检索我最新的推文
$.getJSON("http://twitter.com/statuses/user_timeline/username.json?count=1",
function(data) {
$("#tweet_text").html(data[0].text);
});
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?
这适用于我桌面上的简单HTML文件。但是,一旦从我的localhost(apache)访问该文件,就不会返回任何数据。我想知道Apache的任何部分是否以某种方式阻止了这个请求?还是其他任何想法?
4 个解决方案
#1
7
JavaScript cannot currently make direct requests cross-domain due to the Same-origin Policy.
由于同源策略,JavaScript目前无法跨域直接请求。
You're best bet is probably to look into JSONP for this.
你最好的选择可能是为了研究JSONP。
You can find more information on it from both jQuery:
您可以从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 $.ajax() for more details.如果URL包含字符串“callback =?”在URL中,请求被视为JSONP。有关更多详细信息,请参阅$ .ajax()中有关jsonp数据类型的讨论。
and 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.
回调:可选。仅适用于JSON格式。如果提供,响应将使用具有给定名称回调的JSONP格式。
- Example:
http://search.twitter.com/search.json?callback=foo&q=twitter
Hope this helps.
希望这可以帮助。
Correction...
If status/user_timeline
supports JSONP, it's not documented as such.
如果status / user_timeline支持JSONP,则不会记录它。
You may have to look into setting up a Cross-Domain Proxy to get the data you want.
您可能需要考虑设置跨域代理以获取所需的数据。
更多相关文章
- 将css添加到jquery数据表中的特定单元格
- 使用jQuery解析JSON数据
- 使用jquery从mysql数据库加载数据
- 从JQuery文件输入中获取数据
- 基于JQuery+JSP的无数据库无刷新多人在线聊天室
- 使用AJAX将动态数据传递给mvc控制器
- jquery对json数据进行增删改查的例子
- 尽管referesh,jQuery格式化不适用于ajax数据
- xml格式原样输出到html或是jsp页面