如何在codeigniter中查找是否是Ajax请求?

I have login through ajax to my application. When user before login, try access admin home page(http://localhost/ci3/admin/Adminhome) that time it redirects him to admin login page(http://localhost/ci3/admin/Adminlogin). After login as user and try to access admin home page, he will get success. Now I want to find ajax request. I have tried something like below,

Admin_controller

<?php
class Admin_controller extends CI_Controller{
    function __construct()
    {
        parent::__construct();
        $this->load->model("Adminmodel","",true);   
        $this->load->library('user_agent');

        $adminId =  $this->session->userdata('cp_adminid');

        if($adminId == null){
            if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])=='xmlhttprequest'){
                echo "it is ajax request";
            } else {
                echo "it is not ajax request";
            }
        } else {

        }
    }
?>

admin.js

$('#adminloginform').validate({

    errorClass: 'error',
    validClass: 'valid',
    errorPlacement: function(error, element) {
             error.insertAfter(element);
    },
    rules: {
        username:{
            required: true,

        },
        password:{
            required: true,
            minlength: 6,
        }
    },
    messages:{
        username: {
            required: "Email is required",

    },
        password: {
            required: "Password is required",
            minlength: "Atleast 6 characters",
    }
    },
    submitHandler: function(){
        var admin=$('#adminloginform').serializeArray();

        $.post("<?php echo base_url()?>admin/Adminlogin/auth",admin,function(data){

            if((data.result == 1) && (data.row.view == 1 || data.row.edit == 1 || data.row.add == 1 || data.row.deleteRole == 1 )){
                window.location="<?php echo base_url()?>admin/Adminhome";
            } else if (data.result == 1) {
                window.location="<?php echo base_url()?>admin/Userhomepage";
            } else {
                noty({ text: 'Username or Password is incorrect',type: 'error',modal:true,timeout:1000});
            }


            //$(".simplemodal-close").trigger("click");
        },"json");

    }

});

If you are using it within the application than why not to create a flag. When you call from ajax set it to true and by-default make it false.

class Admin_controller extends CI_Controller{
    function __construct($AjaxFlag = false)
    {
        parent::__construct();
        $this->load->model("Adminmodel","",true);   
        $this->load->library('user_agent');

        $adminId =  $this->session->userdata('cp_adminid');

        if($adminId == null){
            if($AjaxFlag){
                echo "it is ajax request";
            } else {
                echo "it is not ajax request";
            }
        } else {

        }
    }
Check in controller function
   function function_name()
    {
      if(is_ajax_request()){

      } 
    }

    Use helper to check this ajax request
   function is_ajax_request()
   {
    $CI = & get_instance();
       if(!$CI->input->is_ajax_request()) {
          exit('No direct script access allowed');
        }
       else
       {
           return true;
       }

    }