【发布时间】:2015-07-24 09:17:37
【问题描述】:
我想知道在我们响应输出json数据后如何停止执行函数和其他涉及的控制器。
在我这里,
- 我只是在
dashboard控制器中调用test()函数 - 在
dashboard构造函数将执行MY_Login库 - 在
MY_Login库中会检查,是来自ajax 的请求吗?如果是这样,只需向 ajax 响应一些状态并停止执行其他状态,例如dashboard控制器中的test()函数。
Ajax
$.ajax({
type: "POST",
url: "dashboard/test",
async: false,
success: function(res){
//response data
}
});
控制器
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));
}
}
图书馆
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');
}
}
}
}
【问题讨论】:
标签: php jquery ajax codeigniter