特に、FB.api()メソッドを使用する場合、Facebook JS SDKに問題があります。私はJQuery.get()を使ってphpスクリプトを呼び出すことから得られるリストを持っています。そして、リスト項目ごとにFacebookユーザIDが来ます。それぞれ異なるHTMLを持つ3つのタイプの「リスト項目」があり、それぞれが別のユーザーから来るため、各項目に対してFB.api()呼び出しを行う必要があります。ここでFB.api()によって他の変数が未定義になる
は、私が現在働いているコードです:
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
私が持っているように見える問題は、それがギフトとリクエストのための部品に当たったらということで、両方のギフトと配列は "になる要求しますFB.api()のコールバックでラップされていないときにコードが完璧に機能するので、これは奇妙です。そして、奇妙なことに、この問題はNotificationsセクションでは発生していないようです。ギフト、リクエスト、および通知は、JQuery.get()を使って述べたようにデータベースから返されたオブジェクトの配列に過ぎず、api()メソッドでラップするまで問題はありません。
ご協力いただければ幸いです。
乾杯
おかげでレモンを浴びせたが、それらのどれも働きました。APIはfbAsyncInit()メソッドで既に呼び出されていますが、通知部分で機能し、displayPicUrlのアラートを追加して手動でチェックすると、グラフ呼び出しがそのまま動作していることがわかります。しかし、助けてくれてありがとう。 – grammar