[英]Codeigniter: Ajax Login Redirect issue with Ion Auth
我正在尝试在CodeIgniter应用程序中使用Ion Auth框架。
当用户单击链接请求时,将转到Controller方法,并检查用户是否已登录。 如果不是,则重定向到auth\\login
方法。 但是我在success(msg):alert(msg);
获得了登录页面的html success(msg):alert(msg);
Ajax请求
$('img').click(function() {
$.ajax({
url: "<?php echo site_url('gallery/openProductDetail');?>",
type: 'POST',
success: function(msg) {
alert(msg);
}
});
});
控制者
function openProductDetail()
{
if (!$this->ion_auth->logged_in())
{
redirect('auth/login');
} else {
$this->load->view('modal/productdetail');
}
}
登录页面来自原始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
通过上面的内容,我们创建了一个名为$ret
的变量,该变量在执行ajax请求时$ret
对某些数据的引用。 否则,当不使用ajax(不使用javascript)时,将发生标准表单重定向行为。
现在,在成功功能中,我们将检查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.