FB.api调用后重新加载不需要的页面

时间:2013-09-09 04:00:29

标签: javascript facebook coffeescript

使用带有coffeescript的Facebook API时遇到问题。 每次我FB.getLoginStatus且状态为"connected"时,我都会自动调用FB.api("/me", (data)->console.log(data))页面,而不会让我自动重新加载。

另一个问题是,如果在console.log电话后放置一个断点并且注意数据的价值,则会从Facebook收到错误。

我想知道我是否这样做,以及为什么页面重新加载。

以下是该页面的coffeescript代码:

window.fbAsyncInit = ->
  FB.init
    appId: document.getElementById("fb-root").getAttribute("data-app-id")
    channelUrl: document.getElementById("fb-root").getAttribute("data-channel-url")
    status: true,
    cookie: true,
    xfbml: true

  FB.Event.subscribe('auth.login', (response) ->
    window.location = window.location
  )
  FB.Canvas.setAutoGrow()
  FB.getLoginStatus((data) ->
     console.log(data)
     if (data.status == "connected")
      uid = data.authResponse.userID
      accessToken = data.authResponse.accessToken;
      FB.api("/me", (data) ->
        console.log(data) // Here is the last call before the reload
      )
     else if (data.status == "not_authorized")
        console.log("Je suis log a facebook")
        $("#FBConnect").on("click", (e) ->
            FB.login( (response) ->
              if response.authResponse
                console.log('Welcome!  Fetching your information.... ');
                FB.api('/me', (response) ->
                  console.log('Good to see you, ' + response.name + '.');
                )
              else
                console.log('User cancelled login or did not fully authorize.');
            )
        )
      # the user is logged in to Facebook,
      # but has not authenticated your app
     else
        console.log("Je ne suis pas connecte a facebook")
      # the user isn't logged in to Facebook.
  )

PageScript = document.getElementsByTagName("script")[0]
return if document.getElementById("FBScript")
FBScript = document.createElement("script")
FBScript.id = "FBScript"
FBScript.async = true
FBScript.src = "//connect.facebook.net/en_US/all.js"
PageScript.parentNode.insertBefore(FBScript, PageScript)

1 个答案:

答案 0 :(得分:1)

页面重新加载可能是由于代码中的以下行:

FB.Event.subscribe('auth.login', (response) ->
    window.location = window.location
)
auth.login状态从auth更改为unknown时会触发{p> connected,因此当您连接时,代码会执行,并且当您重新加载页面时功能,页面重新加载。删除行:: window.location = window.location应该解决它。