I want to know how to stop execute function and other controller that involved after we response output json data.
In my case here,
- I just call
test()
function indashboard
controller - In
dashboard
constructor will executeMY_Login
library - In
MY_Login
library will check that, Is it request from ajax? if so, just response some status to ajax and stop execute others such astest()
function indashboard
controller.
Ajax
$.ajax({
type: "POST",
url: "dashboard/test",
async: false,
success: function(res){
//response data
}
});
Controller
class Dashboard extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library("MY_Login");
}
public function index()
{
$this->load->view('admin/dashboard_view');
}
public function test(){
// must stop here...
$mydata = array(
"mydata" => "testing"
);
$this->output->set_content_type('application/json')->set_output(json_encode($mydata));
}
}
Library
class MY_Login extends CI_Controller{
function __construct() {
parent::__construct();
// call with constructor.
$this->isLogin();
}
function isLogin() {
// if there is no session existed
if(!$this->session->userdata('USR_NM')){
if($this->input->is_ajax_request()){
$redirect_link = array(
"ajax_redirect_link" => TRUE
);
$this->output->set_content_type('application/json')->set_output(json_encode($redirect_link));
// what i want...
// just response the above json data to ajax
//and stop execute other code below or other controller
}else{
// redirect to login page
redirect('/login', 'refresh');
}
}
}
}