2016-09-19 37 views
1

我正在使用facebook javascript sdk。當我做一個API調用它回來並exectutes錯誤函數,但它返回我請求的數據。即使它返回正確的數據,我想知道爲什麼它執行錯誤函數。你能幫忙嗎?感謝爲什麼我的臉譜圖api調用執行錯誤回調函數

error

的javascript:

window.fbAsyncInit = function() { 
      FB.init({ 
        appId  : '<my_App_id>', 
        cookie  : true, // enable cookies to allow the server to access the session 
        xfbml  : true, // parse social plugins on this page 
        version : 'v2.5' // use graph api version 2.5 

      }); 
      //device APIs are available 
      document.getElementById("facebook_login").addEventListener('click', loginViaFacebook, false); 

      function loginViaFacebook(){ 
       FB.getLoginStatus(function(response) { 
        if (response.status === 'connected') { 
          //user is logged into our app and facebook. 
         //now we need to get their facebook email address and check if they are registered with us. ie their email address exists in the database 

         FB.api("me/?fields=id,email,picture", ["public_profile"],function(result){ 

           alert("Result: " + JSON.stringify(result)); 

           //do ajax request here with email address. 
           }, 
           function(error){  
           //api call failed 
            alert("api call Failed: " + JSON.stringify(error)); 
           } 
         ); 

        }else if(response.status === 'not_authorized'){ 
         //The person is logged into Facebook, but not your app.(facebook docs) 
         //in this case though they are coming from just pressing the facebook_login button on our app so we dont need to tell them to log in. 

         }else{ 
          FB.login(function(response) { 
          // handle the response 
          console.log("UserInfo: " + response.authResponse.userID); 
          if(response.status === 'connected'){ 

          FB.api('/me?fields=id, email, name, picture', ["public_profile"],function(data) { 
           console.log(data); 
           var fb_user_id = data.id; 
           var fb_name = data.name; 
           var fb_email = '[email protected]'; 
           var fb_picture_url = data.picture.data.url; 
           facebook_details.push({'fb_name': fb_name, 'fb_pic': fb_picture_url}); 

           console.log(fb_user_id); 
           $.ajax({ 
            type: 'post', 
             url: 'http://www.example.com/login_with_facebook.php', 
              data: {'fb_user_id': fb_user_id, 'fb_email': fb_email},      

             success: function(data){ 
             alert("data" + data); 

             }, 
             error: function(xhr, status, error) { 
             alert("error message:" + error); 
            } 

           }); //end ajax 

          }, 
          function(error){  
           //api call failed 
           alert("login api call Failed: " + JSON.stringify(error)); 
          }); //end api call 
          }//end if statement 

         }, 
         function(error){ 
           alert("FB login Failed: " + error); 
          }); 


         } 

       }, 
       function(error){ 
        alert("FB get status Failed: " + error); 
       }); 

      }//end loginViaFacebook 



     }; 
     //remove this when packaging with phonegap as we will use the facebookconnectplugin instead. 
     // Load the SDK asynchronously 
      (function(d, s, id) { 
       var js, fjs = d.getElementsByTagName(s)[0]; 
       if (d.getElementById(id)) return; 
       js = d.createElement(s); js.id = id; 
       js.src = "//connect.facebook.net/en_US/sdk.js"; 
       fjs.parentNode.insertBefore(js, fjs); 
      }(document, 'script', 'facebook-jssdk')); 

控制檯: console

開發工具網絡狀態碼302 status code 302

開發工具網絡也狀態碼307

status code 307

+0

你能提供一些信息(響應報頭)什麼樣的錯誤是什麼? 來自Google Developers Tools(Chrome中的F12)/ Network –

回答

1

如果你想擺脫SecurityError,你應該使用HTTPS加密你的流量。這不是那麼難:

  1. 上的CloudFlare創建帳戶
  2. 更改域的DNS到CloudFlare的
  3. 在CloudFlare的控制檯輸入服務器的IP
  4. 使免費HTTPS(和禁用緩存 - 這可能是棘手有時)

現在,您網站的流量將通過CloudFlare重定向。 從您的客戶到CloudFlare的流量將被加密,CloudFlare到您服務器的流量不會被加密。

雖然它不是100%安全的,但它可以保護大多數常見的攻擊媒介 - 惡意網絡提供者等,它需要很少的努力才能獲得免費服務工作者準備好的網站。

如果你想讓自己的證書,您可以使用Let's Encrypt

+0

謝謝。那麼你認爲這就是爲什麼它會返回302和307響應? – Sarah

+0

讓我們試試這個,如果它不起作用,給我一個喊,我們會試着弄清楚這一點:) –

相關問題