0

I am using var userID = (FB.getAuthResponse() || {}).userID; to get ID of logged in user, FB .init is initialized properly. The issue that I am encountering is If I put

var userID = (FB.getAuthResponse() || {}).userID;

inside document.ready(); it is returned as undefined whereas if I put it in some javascript function and call that function on a button click, it works perfectly fine.

Anyone know what could be the issue?

I am also getting this when I try to access it directly on my website:

XMLHttpRequest cannot load {CALL o be MADE}. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '{MY WEBSITE ADDRESS}' is therefore not allowed access. (index)

PS: I don't get the above mentioned error in my facebook page tab, I only get this when I try to access directly my website.

1 Answer 1

1

I suggest using the asynchronous way to check if a user is logged in and to get his id:

You should never use this method at page load time. Generally, it is safer to use FB.getLoginStatus if you are unsure.

Check out the Facebook docs for code and information: https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus

Of course you should call this right after FB.init.

5
  • asynchronous way? yo mean via some ajax call to server?
    – Badddy
    Commented Sep 24, 2014 at 9:55
  • getLoginStatus is asynchronous, just check out the docs. it offers a callback function that is called asynchronously.
    – andyrandy
    Commented Sep 24, 2014 at 10:02
  • thank you, but just wondering, why on click that works and on page load it doesn't
    – Badddy
    Commented Sep 24, 2014 at 10:09
  • it depends when you use it. it must be used after fb.init.
    – andyrandy
    Commented Sep 24, 2014 at 10:15
  • 1
    "i am sure" or "i actually do"? :) - well, getLoginStatus is definitely much better ans recommended by facebook, but feel free to post your code in your question so i can take a look.
    – andyrandy
    Commented Sep 24, 2014 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.