[英]Codeigniter: Ajax Login Redirect issue with Ion Auth
I am trying to use Ion Auth framework with my CodeIgniter application. 我正在尝试在CodeIgniter应用程序中使用Ion Auth框架。
When user clicks a link request goes to Controller method and checks if user is logged in or not. 当用户单击链接请求时,将转到Controller方法,并检查用户是否已登录。 If not then redirects to
auth\\login
method. 如果不是,则重定向到
auth\\login
方法。 But I get html of the login page in my success(msg):alert(msg);
但是我在
success(msg):alert(msg);
获得了登录页面的html success(msg):alert(msg);
Ajax Request Ajax请求
$('img').click(function() {
$.ajax({
url: "<?php echo site_url('gallery/openProductDetail');?>",
type: 'POST',
success: function(msg) {
alert(msg);
}
});
});
Controller 控制者
function openProductDetail()
{
if (!$this->ion_auth->logged_in())
{
redirect('auth/login');
} else {
$this->load->view('modal/productdetail');
}
}
Login Page It is login.pho from original Ion Auth 登录页面来自原始Ion Auth的login.pho
<h1><?php echo lang('login_heading');?></h1>
<p><?php echo lang('login_subheading');?></p>
<div id="infoMessage"><?php echo $message;?></div>
<?php echo form_open("auth/login");?>
<p>
<?php echo lang('login_identity_label', 'identity');?>
<?php echo form_input($identity);?>
</p>
<p>
<?php echo lang('login_password_label', 'password');?>
<?php echo form_input($password);?>
</p>
<p>
<?php echo lang('login_remember_label', 'remember');?>
<?php echo form_checkbox('remember', '1', FALSE, 'id="remember"');?>
</p>
<p><?php echo form_submit('submit', lang('login_submit_btn'));?></p>
<?php echo form_close();?>
<p><a href="forgot_password"><?php echo lang('login_forgot_password');?></a></p>
$ret; // declare a variable to return.
if (!$this->ion_auth->logged_in()){
if($this->input->is_ajax_request()){
/* This is an AJAX request, send the url back to be redirected to. */
$ret['url'] = site_url().'auth/login';
$ret['html'] = FALSE;
}else{
/* This is not ajax, it is a standard form submission, let codeigniter handle it*/
redirect('auth/login');
}
} else {
if($this->input->is_ajax_request()){
$ret['url'] = FALSE; //they are logged in, and this is ajax, don't redirect.
$ret['html'] = $this->load->view('modal/productdetail', '', TRUE); //allow the HTML to be returned to the server
}else{
/* They are logged in, but this is not ajax, redirect them. */
redirect('modal/productdetail');
}
}
echo json_encode($ret); //json_encode our $ret array, and send to browser
With the above, we're creating a variable called $ret
that holds reference to some data when performing ajax requests. 通过上面的内容,我们创建了一个名为
$ret
的变量,该变量在执行ajax请求时$ret
对某些数据的引用。 Otherwise, when not using ajax (no javascript), standard form redirection behavior ensues. 否则,当不使用ajax(不使用javascript)时,将发生标准表单重定向行为。
Now, in our success function, we're going to check for the existence of the data.url
and data.html
properties and act accordingly. 现在,在成功功能中,我们将检查
data.url
和data.html
属性的存在并采取相应的措施。
dataType: 'json', //add this so the success: knows how to parse returned data
success:function(data){
if(data.url){
window.location = data.url;
}else if(data.html){
$('element').html(data.html); // where "element" is an HTML element on your page you wish to override with a partial view
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.