简体   繁体   English

如何在JavaScript中使用JSON格式阅读Facebook Feed

[英]How to read facebook feeds using json format in javascript

This is my fee url in json format here 这是JSON格式我费网址在这里

I want to know how to get the post content,title,likes,comment using jquery getJSON method. 我想知道如何使用jquery getJSON方法获取帖子内容,标题,喜欢,评论。 Any help woulld be appricated ? 有什么需要帮助的吗?

JQUERY CODE JQUERY代码

$("document").ready(function() {
    $.getJSON("https://www.facebook.com/feeds/page.php?id=397319800348866&format=json", function(data) {
        $("#div-my-table").text("<table>");
        $.each(data, function(i, item) {
            $("#div-my-table").append("<tr><td>" + item.EncoderName + "</td><td>" + item.EncoderStatus + "</td></tr>");
        });
        $("#div-my-table").append("</table>");
    });
});

HTML CODE HTML代码

<table id="div-my-table">
    <tr><td></td></tr>
     <tr><td></td></tr>
     <tr><td></td></tr>
</table>

Use jquery getJSON() with a callback=? 使用jquery getJSON()和callback =? so it will use JSONP. 因此它将使用JSONP。 You must use some api like graph graph.facebook.com Something like the below. 您必须使用诸如图graph.facebook.com之类的一些api,如下所示。

$.getJSON('https://graph.facebook.com/397319800348866?callback=?', function(data) {
     console.log(data);
     $('#likes').text(data.likes);

})
.success(function() { console.log('success'); })
.error(function() { console.log('error'); })
.complete(function() { console.log('complete'); });​

HTML 的HTML

 likes: <div id='likes'></div>​

You can't, as the feed is at a different domain to the one you'll be running your Javascript on and you can't do cross-domain requests with AJAX. 您不能这样做,因为Feed与您将在其上运行Javascript的域位于不同的域,并且您无法使用AJAX进行跨域请求。

You'll need to implement a server-side solution using PHP or similar to fetch the data and regurgitate it as JSON and then use your AJAX to load your local, same-domain script. 您将需要使用PHP或类似工具实现服务器端解决方案,以获取数据并将其重新定义为JSON,然后使用AJAX加载本地的同域脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM