gpt4 book ai didi

javascript - 打印 JSON 数据?

转载 作者:行者123 更新时间:2023-12-02 20:23:55 26 4
gpt4 key购买 nike

我正在学习如何使用 JavaScript,因此我编写了一个简单的脚本来打印特定关键字的所有公共(public) Facebook 状态。由于某种原因,它没有按照我的预期进行。可以找到 JSON 的示例 URL 为:http://graph.facebook.com/search?q=Beatles

现在,它没有打印任何结果。谁能发现其中的错误吗?

<input type="text" id="query" /><button>add</button><br />
<div id="results">

</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var url='http://graph.facebook.com/search?q=';
var query;
$('button').click(function(){
query=$("#query").val();
$.getJSON(url+query,function(json){
$.each(json.data,function(i,feed){
if(feed.type=='status') {
$("#results").append('<p>'+feed.message+'</p>');
}
});
});
});
});
</script>

最佳答案

您无法从外部 URL ( same origin policy) ) 检索数据。只能通过 Facebook API 支持的 JSONP 检索数据。您必须将 callback=? 添加到您的 URL 以便使用它:

var url='http://graph.facebook.com/search?callback=?&q=';

(您在对现已删除的答案的评论中链接到的 Twitter 示例中也执行了此操作)

关于javascript - 打印 JSON 数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5132717/

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