2012-04-25 38 views
0

当用户进入特定的.php页面时,我会用FB.ui发送一个请求。如何用Javascript sdk进入页面发送FB请求?

我使用这个脚本异步加载的JavaScript SDK:

<div id="fb-root"></div> 
<script> 
window.fbAsyncInit = function() { 
FB.init({ 
    appId  : 'YOUR_APP_ID', // App ID 
    channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File 
    status  : true, // check login status 
    cookie  : true, // enable cookies to allow the server to access the session 
    xfbml  : true // parse XFBML 
}); 

// Additional initialization code here 
}; 

// Load the SDK Asynchronously 
(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)); 

function sendRequestToRecipients(user_id) { 
    FB.ui({method: 'apprequests', 
     message: 'My Great Request', 
     to: user_id, 
    }, requestCallback); 
    } 
</script> 

然后,我尝试调用函数sendRequestToRecipients()用的onLoad功能,在标签内:

<body onLoad="sendRequestToRecipients('0000'); return false;"> 

但我收到2错误“意外的标识符”,没有任何反应。

另外,如果我从身体中删除的onload和我手动调用sendRequestToRecipients功能与按钮

<input type="button" 
    onclick="sendRequestToRecipients('0000'); return false;" 
    value="Send" 
    /> 

一切顺利完美!

任何帮助?

回答

2

您不能从加载事件中调用该函数,因为您只能在facebook sdk完成加载和初始化后才发出api请求。

它应该是:

<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : 'YOUR_APP_ID', // App ID 
      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File 
      status  : true, // check login status 
      cookie  : true, // enable cookies to allow the server to access the session 
      xfbml  : true // parse XFBML 
     }); 

     sendRequestToRecipients("USER_ID"); 
    }; 

    // Load the SDK Asynchronously 
    (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)); 

    function sendRequestToRecipients(user_id) { 
     FB.ui({method: 'apprequests', 
      message: 'My Great Request', 
      to: user_id, 
     }, requestCallback); 
    } 
</script> 

请注意,我在window.fbAsyncInit回调方法称为sendRequestToRecipients,在FB.init后。