通过jquery在脸书上发布多条帖子

Multiple facebook post via jquery

本文关键字:布多条 jquery 通过      更新时间:2023-09-26

我正试图使用jQuery在一个页面上发布多个facebook post。到目前为止,我成功地发布了第一个facebook post,但无法发布另一个。

发生了什么

facebook帖子嵌入url是

<div id="fb-root">
</div><script>
(function(d, s, id)
 { 
  var js, fjs = d.getElementsByTagName(s)[0]; 
  if (d.getElementById(id)) return; 
  js = d.createElement(s);
  js.id = id;  
  js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.3";    
  fjs.parentNode.insertBefore(js, fjs);
 }
(document, 'script', 'facebook-jssdk'));
</script>
<div class="fb-post" data-href="https://www.facebook.com/ClashofClans/posts/1104473549576967:0" data-width="500">
<div class="fb-xfbml-parse-ignore"><blockquote cite="https://www.facebook.com/ClashofClans/posts/1104473549576967:0">
<p>The update is finally here!!! Maintenance will start soon! Read all that&#039;s new: http://supr.cl/UpdateNotes
</p>
Posted by 
<a href="https://www.facebook.com/ClashofClans">Clash of Clans</a> on&nbsp;
<a href="https://www.facebook.com/ClashofClans/post/1104473549576967:0">Wednesday, July 1, 2015</a>
</blockquote>
</div>
</div>

当我将这个URL直接附加到DIV时,它会调用一个iframe,然后在DOM上完成facebook post数据绑定。它工作得很好,但下次Iframe没有加载到DOM上。在控制台中,我第一次发现应用程序错误,即Invalid App Id: Must be a number or numeric string representing the application id.。我尝试了正确的AppId,但没有成功。以下是我迄今为止尝试过的代码

$("input").blur(function ()
 {
   $('div').html($(this).val());
   //$(this).val() is facebook post URL shown above
 })

有人能给我建议正确的方法吗?

使用以下代码。它会考虑加载代码的时间。答案是使用此源创建的使用异步加载的javascript如果愿意,可以将getLoginStatus调用封装在循环中。

    <!DOCTYPE HTML>
<html>
    <head>
        <title></title>
        <script src="jquery-1.11.1.min.js"></script>
    </head>
    <body>
        <input type="text" id="message" />
        <input type="button" id="send" value="Send" />
    <script>
        $(document).ready(function(){
            function facebookReady() {
                FB.init({
                    appId      : '1521775134725984',
                    xfbml      : true,
                    status   : true,
                    cookie     : true,
                    version    : 'v2.1'
                });
                $(document).trigger("facebook:ready");
            }
            if(window.FB) {
                facebookReady();
            } else {
                window.fbAsyncInit = facebookReady;
            }
        });
        $(document).on("facebook:ready", function() {
            $("#send").on("click", function() {
                FB.getLoginStatus(function(response) {
                    if (response.status === 'connected') {
                        var body = $("#message").val();
                        FB.api('/me/feed', 'post', { message: body }, function(response) {
                            if (!response || response.error) {
                                alert('Error occured');
                            } else {
                                alert('Post ID: ' + response.id);
                            }
                        });
                    }
                    else {
                        FB.login(function(){
                        }, {scope: 'publish_actions'});
                    }
                });
            });
        });
    (function(d, s, id){
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>
    </body>
</html>