pengwin
pengwin

Reputation: 87

Redirect in AJAX

I am using Facebook SDK 4.0 and I have a login page in codeigniter using AJAX to send and receive requests and login users.

I would like to allow users to login using Facebook, but I'm stuck. I have a form with action to controller function named facebook_login. I have a javascript on submit of this form:

$('.facebook_login').submit(function() {       
    var errors = $(this).find('.errors');
    var success = $(this).find('.success');
    var overlay = $(this).find('.overlay');

    overlay.fadeIn(300);
    errors.fadeOut('fast');
    success.fadeOut('fast');

    var url = $(this).attr('action');
    var data = $(this).serialize();

    $.ajax({
        url: url, //url = path to which form should be submitted
        type: 'POST', //POST = form method
        dataType: 'json', //json - data type in which server is supposed to send response
        data: data,
        success: function(response) { //response = reply sent by server after processing our request
            if (response.status == true)
            {
                errors.fadeOut('fast');
                overlay.fadeOut('fast');
                success.html(response.message).fadeIn('fast').delay(300).fadeOut(function(){
                    window.location =response.redirect;
                });
            }
            else
            {
                success.fadeOut('fast');
                overlay.fadeOut();
                errors.html(response.message).fadeIn('fast');
            }
        },
        error: function() {
            overlay.fadeOut();
        }
    });
    return false;
});

The function facebook_login does this:

session_start();
$this->load->library('facebook');

$user_logged_in = $this->facebook->logged_in();

//check if the user is already logged in with facebook
if(!$user_logged_in){
    //if the user is not logged in
     $loginUrl = $this->facebook->login_url();   
}

I want to redirect the user to login to facebook and then gets redirected back to the website, save or session the user and display an error/successful alert using:

header('Content-Type: application/json');
echo json_encode($response);

Upvotes: 0

Views: 434

Answers (2)

pengwin
pengwin

Reputation: 87

Managed by using the JS SDK with the PHP SDK thank you @luschn

Upvotes: 0

Sathya Baman
Sathya Baman

Reputation: 3515

Simply use javascript redirect.

 location.href = "http://some.com/your path";

Upvotes: 1

Related Questions