-2

わかりました、コードを作成しましたが、エラーが発生しました。ポップアップが表示されたら後で試してください。colsole には、undefined at "function callback(response) { console.log(response); }" と表示されます。

コードは次のとおりです。

<script>
function postToFeed() {
  // call the API
  var obj = {
    method: 'feed',
    link: 'https://apps.facebook.com/assault_combat/takegift',
    picture: 'https://flyandsmash.herokuapp.com/images/Gift.png',
    name: 'Take a gift from Assault Combat',
    caption: 'https://apps.facebook.com/assault_combat/',
    actions: [
      {'name': 'Get Gift', 'link': 'https://apps.facebook.com/assault_combat/takegift'}
    ],
    description: 'Action links are awesome.'
  };

  function callback(response) {
    console.log(response);
  }

  FB.ui(obj, callback);
}

</script>

ところで、「FB.initなどと入力すると、それにもエラーが表示されます...(div fbルートなどをインポートしました)

4

1 に答える 1

3

Always use the asynchronous way to load the JavaScript SDK: https://developers.facebook.com/docs/javascript/quickstart

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '{your-app-id}',
      status     : true,
      xfbml      : true
    });

    //earliest possibility to use FB
  };

  (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/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));
</script>

About the error: None of the Links you added in the the obj are reachable, might be the main problem. Also, the caption cannot have and URL, but it should still work with that because Facebook just removes it. It´s better to try with less parameters, remove the action and try it without.

I´d also write it without a nested function:

<script>
function callback(response) {
  console.log(response);
}

function postToFeed() {
  // call the API
  var obj = {
    method: 'feed',
    link: 'https://apps.facebook.com/assault_combat/takegift',
    picture: 'https://flyandsmash.herokuapp.com/images/Gift.png',
    name: 'Take a gift from Assault Combat',
    caption: 'https://apps.facebook.com/assault_combat/',
    actions: [
      {'name': 'Get Gift', 'link': 'https://apps.facebook.com/assault_combat/takegift'}
    ],
    description: 'Action links are awesome.'
  };

  FB.ui(obj, callback);
}
</script>

Make sure you call "postToFeed" AFTER initializing the SDK, and ALWAYS on user interaction/mouse events. Browsers may block the popup if you don´t use it on user events.

于 2013-04-06T16:25:53.297 に答える