2015-09-11 48 views
0

我試圖在Facebook上發佈牆上的圖像,我用下面的代碼誤差函數:如何解決沒有定義

<script type="text/javascript"> 
window.fbAsyncInit = function() { 
    FB.init({ 
    appId  : 'XXXXXXXX', // replace your app id here 
    channelUrl : 'http://localhost/channel.html', 
    status  : true, 
    cookie  : true, 
    xfbml  : true 
    }); 
}; 
(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 FBLogin(){ 
    FB.login(function(response){ 
     if(response.authResponse){ 
      window.location.reload(); 
     } 
    }, {scope: 'email'}); 
} 
function post_on_wall() 
{ 
      var share_name   = 'rohitashav'; 
      var share_link   = 'http://rohitashavsinghal.com'; 
      var share_image   = 'http://rohitashavsinghal.com/fb/love.jpg'; 
      var share_caption  = 'testing'; 
      var share_description = 'test desc'; 

      var share_redirect  = 'http://rohitashavsinghal.com/'; 


      FB.ui(
      { 
       method:   'feed', 
       name:   share_name, 
       link:   share_link, 
       picture:  share_image, 
       caption:  share_caption, 
       description: share_description 
       //message:  '' 
      }, 
      function(response) 
      { 
       if (response && response.post_id) 
       { 
        alert('thanks for sharing!'); 
        window.top.location.href = share_redirect; 
       } 
       else 
       { 
        alert('Post was not published.'); 
       } 
      } 
} 
</script> 

和創建的按鈕如下:

<input type="button" value="Post on Wall" onClick="post_on_wall();" /> 

當我點擊這個按鈕,然後在console我收到以下錯誤:

post_on_wall is not defined雖然我已經定義了這個功能

爲什麼我得到這個錯誤?

回答

1

您的FB.ui呼叫未關閉。回調函數後需要添加);。沒有這個post_on_wall功能是無效的。說實話,我真的很驚訝,你沒有得到有關控制檯的錯誤。

此外,您的呼籲包括Facebook JS有一個錯位)。您需要將}(document));更改爲})(document);

+0

如果我關閉這個然後我得到另一個錯誤'FB沒有定義' –

+0

@RohitashvSinghal找到另一個問題,並更新我的答案 –

1

我注意到您的代碼中存在一些錯誤。

1.

(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));` 

如果我得到我的JavaScript的權利,應該是這樣:

(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); 
  • 內,您的通話FB.UI,你有幾個錯誤:

    FB.ui(//method opened 
        { 
         method:   'feed', 
         name:   share_name, 
         link:   share_link, 
         picture:  share_image, 
         caption:  share_caption, 
         description: share_description 
         //message:  '' 
        }, 
        function(response) 
        { 
         if (response && response.post_id) 
         { 
          alert('thanks for sharing!'); 
          window.top.location.href = share_redirect; 
         } 
         else 
         { 
          alert('Post was not published.'); 
         } 
    
  • 現在,對於打開的方法調用,沒有匹配的大括號)

    相關問題