javascript 使用本地主机测试 Facebook 共享对话框 - “无法解析 URL http://localhost 上的对象”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26021781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing Facebook Share Dialog with localhost - "Unable to resolve object at URL http://localhost"
提问by davidpauljunior
I'm trying to use the current (at date of asking this question) Facebook Share Dialog using just the URL (not the SDK).
我正在尝试仅使用 URL(而不是 SDK)来使用当前(在提出此问题的日期)Facebook 共享对话框。
My JS looks like this:
我的 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;
}
The error I'm getting is:
我得到的错误是:
Could not resolve object at URL
http://localhost/
.
无法解析 URL 处的对象
http://localhost/
。
What does that mean and how do I go about trying to resolve it? I should note that this JS does work without problems using the old 'sharer.php' url for facebook.
这是什么意思,我该如何尝试解决它?我应该注意到,这个 JS 使用旧的 'sharer.php' url for facebook 确实可以正常工作。
I've got http://localhost
added into my app.
我已http://localhost
添加到我的应用程序中。
回答by looggi
Since FB can't access to your localhost to feed the popup, you have to feed it yourself.
由于 FB 无法访问您的本地主机来提供弹出窗口,因此您必须自己提供它。
First, add the Facebook's Javascript SDK on every pages you need it :
首先,在您需要的每个页面上添加 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'));
Then, bind this function on whatever trigger you want, a click event for example :
然后,将此函数绑定到您想要的任何触发器上,例如单击事件:
$('#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);
}
);
});
I console.log the response but in this block you can do what you want. Catch the response to see if the post has been well shared for example.
我 console.log 响应,但在这个块中你可以做你想做的。例如,查看回复以查看帖子是否已被很好地共享。
回答by maxhs
What @WizKid is trying to say is that Facebook is looking for some meta tags on your page (e.g. <meta property="og:title" content="My awesome site" />
, so that it can structure the content of your share... but isn't finding them. Your current share may work on production, but won't on localhost. You can test to see what facebook is looking for with their url debugger: https://developers.facebook.com/tools/debug/
@WizKid 想说的是 Facebook 正在您的页面上寻找一些元标记(例如<meta property="og:title" content="My awesome site" />
,以便它可以构建您分享的内容......但没有找到它们。您当前的分享可能会用于生产,但不会在本地主机上。您可以使用他们的 url 调试器测试以查看 facebook 正在寻找什么:https: //developers.facebook.com/tools/debug/
The old URL-parameter-based sharing works with localhost (if you've added it as a site URL and app domain) because all facebook had to do in that instance was parse the query parameters of your share string.
旧的基于 URL 参数的共享适用于 localhost(如果您已将其添加为站点 URL 和应用程序域),因为在该实例中 facebook 必须做的就是解析您的共享字符串的查询参数。
回答by Harsha
To test facebook share debugger on localhost we can create a tunnel.
要在本地主机上测试 facebook 共享调试器,我们可以创建一个隧道。
Follow the below steps
source: facebook share debugger on localhost
按照以下步骤
来源:facebook share debugger on localhost