简体   繁体   English

FB.ui没有回调

[英]No callback with FB.ui

I can't get the Facebook UI callback to run. 我无法让Facebook UI回调运行。 I've used the example from the FB dev site: 我使用了FB开发人员站点中的示例:

https://developers.facebook.com/docs/reference/dialogs/feed/ https://developers.facebook.com/docs/reference/dialogs/feed/

FB.init({appId: "YOUR_APP_ID", status: true, cookie: true});

function postToFeed() {

    // calling the API ...
    var obj = {
      method: 'feed',
      redirect_uri: 'YOUR URL HERE',
      link: 'https://developers.facebook.com/docs/reference/dialogs/',
      picture: 'http://fbrell.com/f8.jpg',
      name: 'Facebook Dialogs',
      caption: 'Reference Documentation',
      description: 'Using Dialogs to interact with users.'
    };

    function callback(response) {
      document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
    }

    FB.ui(obj, callback);
}

When I run it on my site I get no callback https://www.ipassexam.com/page/fb-test 当我在我的网站上运行它时,我得不到回叫https://www.ipassexam.com/page/fb-test

Any insight would be appreciated. 任何见解将不胜感激。

Get rid of the redirect_uri parameter. 摆脱redirect_uri参数。 This works for me every time: 这对我来说每次都适用:

FB.ui({
    method: 'feed',  
    link: "http://...",
    name: "Some Title",
    caption: "Some Caption",
    description: "Some Description",
    picture: "http://..."
}, function(response) {
    if (response && response.post_id) {
        alert('Post was published.');
    } else {
        alert('Post was not published.');
    }
});

Also make sure that there are no errors in the console. 还要确保控制台中没有错误。 But i did not see any error anyway. 但是我还是没有看到任何错误。

Spent 2 hours figuring the problem and the solution is, mention the display property (popup in my case). 花了2个小时来解决问题和解决方案,提到显示属性(在我的情况下弹出)。

function postToFeed() {

// calling the API ...
var obj = {
  method: 'feed',
  **display: 'popup',**
  link: 'https://developers.facebook.com/docs/reference/dialogs/',
  picture: 'http://fbrell.com/f8.jpg',
  name: 'Facebook Dialogs',
  caption: 'Reference Documentation',
  description: 'Using Dialogs to interact with users.'
};

function callback(response) {
  document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
}

FB.ui(obj, callback);

} }

Tip: redirect_uri is not mandatory if display is explicitly specified as popup. 提示:如果显式指定为弹出窗口,则redirect_uri不是必需的。

Try to set display attribute with "popup" with the request as below: 尝试通过请求将显示属性设置为“弹出”,如下所示:

FB.ui({
    method: 'feed',  
    link: "http://...",
    name: "Some Title",
    caption: "Some Caption",
    description: "Some Description",
    display: "popup",
    picture: "http://..."
}, function(response) {
    if (response && response.post_id) {
        alert('Post was published.');
    } else {
        alert('Post was not published.');
    }
});

I have encountered this issue inside the FB canvas page when I tried to post using feed method and I always got the "undefined" response. 当我尝试使用feed方法发布时,我在FB画布页面中遇到了这个问题,我总是得到“未定义”的响应。 I have tried the above attribute and now it is working. 我已经尝试了上述属性,现在可以正常工作了。

Perhaps you should try using an anonymous function as a callback - 也许你应该尝试使用匿名函数作为回调 -

FB.ui(obj, function(response) {
  document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
});

try to add this before FB.init 尝试在FB.init之前添加它

window.fbAsyncInit = function() { //put FB.init here }

 (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = '//connect.facebook.net/en_US/all.js';
   ref.parentNode.insertBefore(js, ref);
 }(document));

so that it will became like this 这样它就会变成这样

window.fbAsyncInit = function() {
 FB.init({appId: "YOUR_APPID", status: true, cookie: true});
};

(function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = '//connect.facebook.net/en_US/all.js';
   ref.parentNode.insertBefore(js, ref);
 }(document));

It might be not your case, but I was looking for a reason of a similar behaviour almost a week and finally I have found that FB.ui was trying to create the iframe to display on the page and for this was creating this iframe under div with id=fb-root. 这可能不是你的情况,但我正在寻找类似行为差不多一周的原因,最后我发现FB.ui试图创建iframe以显示在页面上,因为这是在div下创建这个iframe id = fb-root。 Unfortunately, this div was inside another div which I myself made invisible! 不幸的是,这个div在另一个div中,我自己也看不见了! So - to cut the long story short - check if you have div with id=fb-root and if this div is visible 所以 - 长话短说 - 检查你是否有id = fb-root的div以及这个div是否可见

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

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