使用localhost测试Facebook Share对话框 - "无法在URL http:// localhost处解析对象"

dav*_*ior 8 javascript facebook

我正在尝试使用当前(在询问此问题的日期)Facebook Share Dialog仅使用URL(而不是SDK).

我的JS看起来像这样:

openFacebookPopup : function (url) {
        this.openSharerWindow('https://www.facebook.com/dialog/share' + '?app_id=145634995501895' + '&display=popup' + '&href=http%3A%2F%2Flocalhost' + '&redirect_uri=http%3A%2F%2Flocalhost');
        return false;
}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

无法解析URL上的对象http://localhost/.

这意味着什么,我该如何解决它?我应该注意,这个JS使用旧的'sharer.php'网址为facebook工作没有问题.

我已http://localhost加入我的应用程序.

小智 7

由于FB无法访问您的localhost来提供弹出窗口,因此您必须自己提供.

首先,在您需要的每个页面上添加Facebook的Javascript SDK:

  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'your-app-id',
      xfbml      : true,
      version    : 'v2.3'
    });
  };

  (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'));
Run Code Online (Sandbox Code Playgroud)

然后,将此函数绑定到您想要的任何触发器上,例如:单击事件:

$('#fb-share-button').click(function() {
    FB.ui({
          method: 'feed',
          link: "The link you want to share", 
          picture: 'The picture url',
          name: "The name who will be displayed on the post",
          description: "The description who will be displayed"
        }, function(response){
            console.log(response);
        }
    );
});
Run Code Online (Sandbox Code Playgroud)

我console.log响应,但在这个块你可以做你想要的.例如,捕获响应以查看帖子是否已被很好地共享.


max*_*xhs 5

@WizKid试图说的是Facebook正在寻找你网页上的一些元标记(例如<meta property="og:title" content="My awesome site" />,它可以构建你的共享内容......但是没有找到它们.你当前的份额可能适用于制作,但不会在localhost上.您可以使用他们的url调试器测试看看facebook正在寻找什么:https://developers.facebook.com/tools/debug/

旧的基于URL参数的共享与localhost一起使用(如果您已将其添加为站点URL和应用程序域),因为所有Facebook必须在该实例中进行解析您的共享字符串的查询参数.