简体   繁体   中英

Different Action as per facebook login status

I want to perform three actions.

  1. If user alread logged in; redirect to new page without pageload using ajax
  2. If logged in but not authorized with my app then tell him to do
  3. If not logged in view login button.

Here is my try:

if (response.status === 'connected') {

    $.ajax({
 type: "POST",
 url: "url.php",
 success: function(r) 
  {
    window.location = 'url.php';   // **This ajax code does not work. What is the error?**
  },
});

  } else if (response.status === 'not_authorized') {

//What to do here to ask for subscription with my ID?    

  } else {

  }
 });

My actual code:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head> </head>
<body style="height: 560px">

<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
    FB.init({
        appId      : '132292862', // Set YOUR APP ID
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true,  // parse XFBML
        oauth        : true
    });

    FB.Event.subscribe('auth.authResponseChange', function(response) 
    {
    if (response.status === 'connected') 
        {
        document.getElementById("log").innerHTML =  "You're connected to Facebook <a href='#' onClick='logout()'>log out</a>";
        document.getElementById("status").innerHTML='';}    
        else if (response.status === 'not_authorized') 
        {
        document.getElementById("log").innerHTML =  "Connection failed";
        } 
        else 
        {
        document.getElementById("log").innerHTML =  "You're logged out";
        }
    }); 
};

(function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return; alert('ok');}
     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));
</script>
</body>
</html>

try this:

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId      : 'myId', // Set YOUR APP ID
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true,  // parse XFBML
        oauth        : true
    });

    FB.Event.subscribe('auth.authResponseChange', function(response) 
    {
    if (response.status === 'connected') 
        {
        document.getElementById("log").innerHTML =  "You're connected to Facebook <a href='#' onClick='logout()'>log out</a>";
        document.getElementById("status").innerHTML='';}    
        else if (response.status === 'not_authorized') 
        {
        document.getElementById("log").innerHTML =  "Connection failed";
        } 
        else 
        {
        document.getElementById("log").innerHTML =  "You're logged out";
        }
    }); 
};

(function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return; alert('ok');}
     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));
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM