I’ve added a constructor in a class to check if some user is logged in.
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->library('encryption');
if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
redirect('account/login');
}
}
Here’s the ajax
$.ajax({
type:'POST',
url:'<?php echo base_url("cart/addToCart"); ?>',
dataType: 'text',
data:{'id':val},
success:function(data) {
if (data != 'added') {
alert('Opps! something went wrong, please try again');
}
}
});
but if I try to call this request without session, it’s not redirecting to login page but giving the whole login page code which I can see in network tab
3
Answers
Add
return
to your redirect:I think the problem arises due to
ajax
request not being completed. When the redirection happens it continues to treat it asajax
request, hence it shows the new page in the network tab.To solve this you’ll have to redirect it to any other function and send a response from it but as you are checking the session in
constructor
, we’ll have to check a condition for that particularfunction(method)
.AJAX
Controller
Hope this helps you.
As I know, Ajax simply return a response text. It will not proccess the request
You can try something like following
AJAX
Controller