簡體   English   中英

codeigniter 中的 ajax 請求返回 html 代碼而不是重定向

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

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

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

但是如果我嘗試在沒有 session 的情況下調用此請求,它不會重定向到登錄頁面,而是提供我可以在網絡選項卡中看到的整個登錄頁面代碼

據我所知,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

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

return添加到您的重定向:

return redirect('account/login');

我認為問題的出現是由於ajax請求未完成。 當重定向發生時,它繼續將其視為ajax請求,因此它在網絡選項卡中顯示新頁面。
要解決此問題,您必須將其重定向到任何其他 function 並從中發送響應,但是當您在constructor中檢查 session 時,我們必須檢查該特定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
        }
    }
});

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

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM