2011-07-21 14 views
0

特に、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()メソッドでラップするまで問題はありません。

ご協力いただければ幸いです。

乾杯

答えて

0

彼らはあなたがFacebookのエラーを取得し、その後FB.api()に包まれたときにのみ定義されていない場合。そのハード正確な原因を特定するので、これは、起こることができますが、私はこれらに対処するいくつかの方法を捨てるだろうと多くの理由があります。

  • あなたグラフコールにアクセストークンを追加し、いくつかは、 facebookコールはそれを必要とします。

  • アプリケーションに正しいfacebookコードがあることを確認する必要があります。 HTMLの場合は、次のとおりです

    ます。また、コールの一部document.readyの並べ替えにこのコードを含める必要がありますどのように問題がある可能性があり

    //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は異なり、一つだけが動作します)

Facebook Developers page

確認Canvas URLTab URLマッチ「Facebook上」の下にもいることを確認し正確でなければなりません

  • グラフ呼び出しが正しくありません。コードにalert(displayPicUrl);コールを追加してみてください。エラーが表示されることがあります。それでも問題が解決しない場合は、グラフのURLコールを再現して、それを自分自身に入力して、それが何を返すかを確認してください。答えのために、これらの作業の

うまくいけば、1

+0

おかげでレモンを浴びせたが、それらのどれも働きました。APIはfbAsyncInit()メソッドで既に呼び出されていますが、通知部分で機能し、displayPicUrlのアラートを追加して手動でチェックすると、グラフ呼び出しがそのまま動作していることがわかります。しかし、助けてくれてありがとう。 – grammar

関連する問題