我正在使用FacebookSDK4.0。我在代码点火器中有一个登录页面,使用Ajax发送和接收请求、登录用户。
我想让用户使用Facebook登录,但我没找到方法。我有一个控制器函数表单,名为facebook_login。我在递交这份表格时有一份javascript代码:
$('.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;
});
函数facebook_login执行以下操作:
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();
}
我想重定向用户以登录到Facebook,然后重定向回网站,保存会话,并使用以下命令显示错误/成功警报:
header('Content-Type: application/json');
echo json_encode($response);