如何停止响应AJAX codeigniter后执行其它控制器 [英] How to stop execute other controller after response to ajax codeigniter
本文介绍了如何停止响应AJAX codeigniter后执行其它控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何停止执行函数和参与后,我们响应输出JSON数据的其他控制器。
在我这里的情况,
- 在我刚刚打电话
测试()
函数仪表板
控制器 - 在
仪表板
构造将执行MY_Login
库 - 在
MY_Login
库将检查,它是从Ajax请求?如果是这样,只是反应进行了一些状态,AJAX和停止执行其他的如测试()
在仪表板
控制器功能。
阿贾克斯
$。阿贾克斯({
键入:POST,
网址:仪表板/测试,
异步:假的,
成功:函数(RES){
//响应数据
}
});
控制器
类仪表盘扩展是CI_Controller {
公共职能__construct()
{
父:: __结构();
$这个 - >负载>库(MY_Login);
}
公共功能指数()
{
$这个 - >负载>查看(管理/ dashboard_view');
}
公共功能测试(){
//必须停止在这里...
$ MYDATA =阵列(
MYDATA=> 测试
);
$这个 - >输出 - > set_content_type(应用/ JSON) - > set_output(json_en code($ MYDATA));
}
}
库
类MY_Login扩展是CI_Controller {
功能__construct(){
父:: __结构();
//用构造函数中调用。
$这个 - > isLogin();
}
功能isLogin(){
//如果没有会话存在
如果($这个 - >!会话级>用户数据(USR_NM')){
如果($这 - >输入 - > is_ajax_request()){
$ redirect_link =阵列(
ajax_redirect_link=>真正
);
$this->output->set_content_type('application/json')->set_output(json_en$c$c($redirect_link));
// 我想要的是...
//只是响应上面的JSON数据阿贾克斯
//并停止执行以下或其他控制器等code
}其他{
//重定向到登录页面
重定向('/登录','刷新');
}
}
}
}
解决方案
HI如果每次你想要做同样的事情,就像你说的,你可以这样做:
库:
如果($这个 - >输入 - > is_ajax_request())
{
$ redirect_link =阵列(ajax_redirect_link=> TRUE);
标题(内容类型:应用程序/ x-json的;字符集= UTF-8);
回声(json_en code($ redirect_link));
出口;
}
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');
}
}
}
}
解决方案
HI if every time you want to do the same thing as you said you can do that :
Library :
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;
}
这篇关于如何停止响应AJAX codeigniter后执行其它控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文