【问题标题】:How to stop execute other controller after response to ajax codeigniter响应ajax codeigniter后如何停止执行其他控制器
【发布时间】:2015-07-24 09:17:37
【问题描述】:

我想知道在我们响应输出json数据后如何停止执行函数和其他涉及的控制器。

在我这里,

  1. 我只是在dashboard控制器中调用test()函数
  2. dashboard 构造函数将执行MY_Login
  3. 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


    【解决方案1】:

    您好,如果您每次都想做与您说的一样的事情,那么您可以这样做:

    图书馆:

    if($this->input->is_ajax_request())
    {
        $redirect_link = array("ajax_redirect_link" => TRUE);
        header('Content-Type: application/x-json; charset=utf-8');
        echo(json_encode($redirect_link));
        exit;
    }
    

    【讨论】:

    • 在我的情况下,如果 is_ajax_request() 则将 json 数据输出到 ajax 但它不起作用...
    • 好的,但如果不是 is_ajax_request() 那么你想停止执行吗?
    • 不,如果 is_ajax_request() 则响应 json 数据并停止下面的其他代码和涉及的控制器。 (以上示例)
    • 接受答案?对不起,我是新手。你能指导我找到它吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 2013-04-30
    相关资源
    最近更新 更多