简体   繁体   English

codeigniter 中的 ajax 请求返回 html 代码而不是重定向

[英]ajax request in codeigniter returning html code instead of redirecting

I've added a constructor in a class to check if some user is logged in.我在 class 中添加了一个构造函数来检查是否有用户登录。

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

Here's the ajax这是 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');
              }
            }
        });

but if I try to call this request without session, it's not redirecting to login page but giving the whole login page code which I can see in network tab但是如果我尝试在没有 session 的情况下调用此请求,它不会重定向到登录页面,而是提供我可以在网络选项卡中看到的整个登录页面代码

As I know, Ajax simply return a response text.据我所知,Ajax 只是返回一个响应文本。 It will not proccess the request它不会处理请求

You can try something like following您可以尝试以下操作

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

Controller Controller

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
       }
}

Add return to your redirect:return添加到您的重定向:

return redirect('account/login');

I think the problem arises due to ajax request not being completed.我认为问题的出现是由于ajax请求未完成。 When the redirection happens it continues to treat it as ajax request, hence it shows the new page in the network tab.当重定向发生时,它继续将其视为ajax请求,因此它在网络选项卡中显示新页面。
To solve this you'll have to redirect it to any other function and send a response from it but as you are checking the session in constructor , we'll have to check a condition for that particular function(method) .要解决此问题,您必须将其重定向到任何其他 function 并从中发送响应,但是当您在constructor中检查 session 时,我们必须检查该特定function(method)的条件。

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

Controller Controller

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
    }
}

Hope this helps you.希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM