当用户进入特定的.php页面时,我会向FB.ui发送请求。
我使用此脚本异步加载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>
然后,我尝试使用onLoad函数调用标记内的函数sendRequestToRecipients():
<body onLoad="sendRequestToRecipients('0000'); return false;">
但我收到2个错误“意外的标识符”,没有任何反应。
此外,如果我从正文中删除onload并使用按钮手动调用sendRequestToRecipients函数
<input type="button"
onclick="sendRequestToRecipients('0000'); return false;"
value="Send"
/>
一切都很完美!
任何帮助?
答案 0 :(得分:2)
你不能从 onload 事件中调用它,因为你只能在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>
请注意,我在 FB.init 之后的 window.fbAsyncInit 回调方法中调用了 sendRequestToRecipients 。