codeigniter failed when use is_ajax_request function on ajax request

I’m trying to update user password from panel admin with ajax request is use is_ajax_request() on the controller but this function says I’m not in ajax request. why does this happen?

Here’s my ajax jquery:

$.ajax({
    url: urlTarget,
    traditional: true,
    type: 'post',
    dataType: 'json',
    data: {currentPassword:currentPassword, newPassword:newPassword},
    success: function ( result ) {
        if( result.status == 'success' ){
            window.location = baseUrl;
        }else{
            $("#login-invalid-input").show();
            $("#login-submit").attr("disabled", false);
        }
    },
    error: ajax_error_handling
});

on the controller:

    private function ajax_checking(){
        if ( !$this->input->is_ajax_request() ) {
            redirect( base_url() );
        }
    }

    public function change_password(){
        $this->ajax_checking();
        $this->load->model("admin_model");

        $postData = $this->input->post();
        $id = $this->session->userdata('id_admin');

        if($this->admin_model->check_current_password($id, $postData['currentPassword'])){
            $this->admin_model->change_password($id, $postData['newPassword']);
            $send["status"] == "success";
        }else
            $send["status"] == "fail";

        echo json_encode($send);
    }

since the result of ajax_checking is false the system redirect me to the set url. Anyone can help me?

Answer

thanks for your help smit, here’s the problem that redirect me to baseUrl :

i set this on the controller, it happens because i have this script on the controllers that redirect me to the basicUrl:

    public function __Construct() {
        parent::__Construct();
        if($this->session->userdata('logged_in') ) {
            redirect( base_url() );
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *