【问题标题】:ajax request in codeigniter returning html code instead of redirectingcodeigniter中的ajax请求返回html代码而不是重定向
【发布时间】:2020-05-13 16:12:41
【问题描述】:

我在一个类中添加了一个构造函数来检查是否有用户登录。

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');
    }
}

这里是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');
              }
            }
        });

但如果我尝试在没有会话的情况下调用此请求,它不会重定向到登录页面,而是提供我可以在network tab 中看到的整个登录页面代码

【问题讨论】:

  • 你不能通过服务器端做ajax重定向。

标签: php jquery ajax codeigniter


【解决方案1】:

据我所知,Ajax 只是返回一个响应文本。它不会处理请求

你可以试试下面的方法

AJAX

$.ajax({
            type:'POST',
            url:'<?php echo base_url("cart/addToCart"); ?>',
            dataType: 'text',
            data:{'id':val, 'type':'ajax'}, //say this request coming from a ajax
            success:function(data) {
              if (data != 'added') {
                 if(data == "not_log_in"){ //checking whether login or not
                       window.location = "login_page";
                 }else{
                       alert('Opps! something went wrong, please try again');
                 }
              }
            }
        });

控制器

if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
       if(isset($_POST["type"])){ // checking whether request coming from ajax or not
           echo "not_log_in"; // if it is ajax, simply show an error massage
       }else{
            redirect('account/login'); //otherwise redirect
       }
}

【讨论】:

    【解决方案2】:

    return 添加到您的重定向中:

    return redirect('account/login');
    

    【讨论】:

    • @fraggely 我在redirect()之前添加了return,它仍然没有重定向
    • 你确定你正在通过 if 语句吗?将 if 语句更改为 100% 为真(或完全删除)以测试重定向。如果可行,那么问题出在您的 if 语句
    • 我已经删除了构造函数中的 if 语句,但它仍然没有重定向,我不明白我在哪里搞砸了
    【解决方案3】:

    我认为问题的出现是由于ajax 请求未完成。当重定向发生时,它会继续将其视为ajax 请求,因此它会在网络选项卡中显示新页面。
    为了解决这个问题,您必须将其重定向到任何其他函数并从中发送响应,但是当您检查 constructor 中的会话时,我们必须检查该特定 function(method) 的条件。

    AJAX

    $.ajax({
        type:'POST',
        url:'<?php echo base_url("cart/addToCart/some_new_function"); ?>',  // make a new function {some_new_function}
        dataType: 'json',  // expects json to be returned
        data:{'id':val},
        success:function(data) {
            if (data == 1) { // fail
                window.location.href = "<?php echo base_url('account/login'); ?>"; // your-location-&-logic
            }else{
                // whatever-you-want-to-do
            }
        }
    });
    

    控制器

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('encryption');
    
        $method = $this->router->fetch_method(); // get method name
    
        if($method !== 'some_new_function'){     
            if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
                redirect('account/login');
            }
        }
    }
    
    function some_new_function(){  // control will come here from ajax after construct
        //perform session check here
         if (!$this->session->userdata('id') || $this->session->userdata('role') != 'CUSTOMER') {
            echo 0; // fail
        }else{
            echo 1; // success // perform-your-task-here
        }
    }
    

    希望这对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-19
      • 2020-11-12
      相关资源
      最近更新 更多