我在PHP中获得了以下代码:
<?php
$json = file_get_contents('https://graph.facebook.com/192655950766049');
$data = json_decode($json, true);
echo $data['description'];
?>
我希望JQuery中的等效代码。 我试着自己做,但我没有运气。 这是我多次尝试之一:
<script type="text/javascript" src="../scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$.getJSON("https://graph.facebook.com/192655950766049", function(json) {
alert("JSON Data: " + json.description);
});
</script>
我从http://api.jquery.com/jQuery.getJSON/读到了解释,但我真的不明白..
无论如何,如果你能帮助我,那将是非常好的!
由于
答案 0 :(得分:3)
您通过使用Javascript请求其他域名来违反Same origin policy。您需要执行此服务器端(即在您的情况下使用PHP)。
答案 1 :(得分:1)
$(document).ready(function(){
var url = "https://graph.facebook.com/192655950766049?limit=3&callback=?";
$.getJSON(url,function(json){
var html = "<ul>";
$.each(json.data,function(i,fb){
html += "<li>" + fb.message + "</li>";
});
html += "</ul>";
$('.facebookfeed').html(html);
});
});
您也可以查看此网址。 http://www.prettyklicks.com/blog/making-a-facebook-feed-using-the-graph-api-json-and-jquery/291/
同样使用我指定的相同方法检查此链接。 using $.getJSON within a loop
答案 2 :(得分:1)
由于cross site scripting限制,您将无法在客户端直接从Facebook下载数据。
答案 3 :(得分:1)
您需要使用jquery的jsonp请求。请参阅this关于如何使用此与Facebook交互的简短讨论。 JSONP允许XSS忽略相同的原始策略。
基本上,您的代码看起来与此类似:
<script type="text/javascript" src="../scripts/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$.getJSON("https://graph.facebook.com/192655950766049?callback=?", function(json) {
alert("JSON Data: " + json.description);
});
</script>
答案 4 :(得分:0)
如果有人有兴趣在本地使用jQuery IE:WAMP,这是一个经过测试的示例。.正在读取的z.txt文件中有一个简单的单号。.
<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$.getJSON("z.txt", function(json) {
alert(json);
});
});
</script>