0

我在使用带有咖啡脚本的 Facebook api 时遇到问题。每次我这样做FB.getLoginStatus并且状态为"connected"时,我都会自动调用FB.api("/me", (data)->console.log(data))页面重新加载而无需我要求。

console.log另一个问题是,如果在调用后放置断点并观察数据的值,则会从 Facebook 获取错误。

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

这是该页面的咖啡脚本代码:

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)
4

1 回答 1

1

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

FB.Event.subscribe('auth.login', (response) ->
    window.location = window.location
)

auth.loginauth当状态从unknown变为时触发connected,因此当您连接时,代码会执行,并且当您在该函数中重新加载页面时,页面会重新加载。删除 line::window.location = window.location应该可以解决它。

于 2013-09-09T04:06:31.483 回答