Javascript 导致其他变量未定义的FB.api()
我在使用FacebookJSSDK时遇到了一些问题,特别是在使用FB.api()方法时。我有一个列表,它是通过使用JQuery.get()调用php脚本来填充的,每个列表项都有一个Facebook用户ID。有三种类型的“列表项”每个都有不同的HTML,我需要为每个项调用FB.api(),因为每个项都来自不同的用户 以下是我目前正在使用的代码:Javascript 导致其他变量未定义的FB.api(),javascript,jquery,facebook-graph-api,Javascript,Jquery,Facebook Graph Api,我在使用FacebookJSSDK时遇到了一些问题,特别是在使用FB.api()方法时。我有一个列表,它是通过使用JQuery.get()调用php脚本来填充的,每个列表项都有一个Facebook用户ID。有三种类型的“列表项”每个都有不同的HTML,我需要为每个项调用FB.api(),因为每个项都来自不同的用户 以下是我目前正在使用的代码: function( data ){ // Parse the json data var parsed = jQuer
function( data ){
// Parse the json data
var parsed = jQuery.parseJSON( data );
// Create arrays for each message type
notifications = parsed.notifications;
gifts = parsed.gifts;
requests = parsed.requests;
// Counter and message to add
var i = 0;
var message = '';
var userData;
var displayPicUrl = '';
//
// NOTIFICATIONS
//
if( notifications && notifications.length > 0 ) {
// Loop through and create a new list item for each notification
for( i = 0; i < notifications.length; i++ ) {
// Get originator user data
FB.api( notifications[i].originator, function( response ) {
userData = response;
displayPicUrl = "http://graph.facebook.com/"+userData.id+"/picture";
message = '<li class="message">' +
'<img src="'+displayPicUrl+'" width="50" height="50" alt="Not one of the five birds I know." title="Not one of the five birds I know" />'+
'<p class="messageText">'+notifications[i].message+'.</p>' +
'<button class="acceptButton">Ok</button>' +
'</li>';
document.getElementById( 'notifications' ).innerHTML += message;
});
} //end loop
} //end if
//
// GIFTS
//
if( gifts && gifts.length > 0 ) {
// Loop through and create a list item for each gift
for( i = 0; i < gifts.length; i++ ) {
FB.api( gifts[i].originator, function( response ) {
if( !response || response.error ) {
alert( 'An error occured retrieving gifts')
} else {
userData = response;
displayPicUrl = "http://graph.facebook.com/"+userData.id+"/picture";
message = '<li class="message">' +
'<img src="'+displayPicUrl+'" width="50" height="50" alt="Not one of the five birds I know." title="Not one of the five birds I know" />'+
'<img class="giftImage" src="'+gifts[i].url+'" width="50" height="50" title="'+gifts[i].name+'" alt="'+gifts[i].name+'" />' +
'<p class="messageText">'+gifts[i].message+'</p>' +
'<button class="declineButton giftDecline">Decline</button>' +
'<button class="acceptButton giftAccept">Accept Gift</button>' +
'<span style="display:none;" id="giftId">'+gifts[i].giftId+'</span>' +
'</li>';
document.getElementById( 'gifts' ).innerHTML += message;
}
});
}
} // end if
//
// REQUESTS
//
if( requests && requests.length > 0 ) {
// Loop through and create a list item for each request
for( i = 0; i < requests.length; i++ ) {
FB.api( requests[i].originator, function( response ) {
if( !response || response.error ) {
alert( 'An error occured retrieving Requests' );
} else {
userData = response;
displayPicUrl = "http://graph.facebook.com/"+userData.id+"/picture";
message = '<li class="message">' +
'<img src="'+displayPicUrl+'" width="50" height="50" alt="Not one of the five birds I know." title="Not one of the five birds I know" />'+
'<img class="giftImage" src="'+requests[i].url+'" width="50" height="50" />' +
'<p class="messageText">'+requests[i].message+'</p>' +
'<button class="declineButton requestDecline">Decline</button>' +
'<button class="acceptButton requestAccept">'+requests[i].okButtonLabel+'</button>' +
'</li>';
document.getElementById( 'requests' ).innerHTML += message;
}
});
}
} // end if
函数(数据){
//解析json数据
var parsed=jQuery.parseJSON(数据);
//为每种消息类型创建数组
通知=parsed.notifications;
礼物=解析的礼物;
requests=parsed.requests;
//计数器和要添加的消息
var i=0;
var消息=“”;
var用户数据;
var displayPicUrl='';
//
//通知
//
如果(通知和通知长度>0){
//循环浏览并为每个通知创建一个新的列表项
对于(i=0;i
我似乎遇到的问题是,一旦它命中了礼物和请求的部分,礼物和请求数组都会变得“未定义”,这很奇怪,因为当代码没有包装在FB.api()的回调中时,它工作得非常好…而且,奇怪的是,通知部分似乎没有出现这个问题。礼物、请求和通知只是从数据库返回的对象数组,正如我使用JQuery.get()所说的,在我将这些内容包装到api()方法之前,没有任何问题
任何帮助都将不胜感激
欢呼声如果它们只有在包装在
FB.api()
中时才被定义,那么你就会得到一个facebook错误。发生这种情况的原因有很多,因此很难找出确切的原因,但我会给出一些处理方法:
- 在图形调用中添加访问令牌,有些facebook调用需要它
- 您需要确保您的应用程序中包含正确的facebook代码。对于HTML,包括:
您还需要在一些
文档中包含此代码。准备好了
类调用:
//Facebook iFrame include window.fbAsyncInit = function () { FB.init({ appId: AppID, status: true, cookie: true, xfbml: true }); FB.Canvas.setAutoResize(); }; (function () { var e = document.createElement('script'); e.async = true; e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js'; document.getElementById('fb-root').appendChild(e); } ());
- 您在facebook中的设置方式可能有问题。请确保所有URL都匹配。如果不匹配,则您的应用程序将抛出错误。这必须是准确的(www.ex.com和ex.com不同,只有一个可用)
画布URL
和选项卡URL
也匹配
- 您的图形调用错误。请尝试向代码中添加一个
调用,它可能会显示您遇到的错误。如果不起作用,请尝试重新生成图形url调用,并在您自己中输入,以查看返回的结果警报(displayPicUrl);
希望其中有一项工作谢谢你的回答,但是没有一项是有效的