duandianwen1723 2017-02-16 11:11
浏览 50

如何在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");

    }

});
  • 写回答

2条回答 默认 最新

  • dongzan0108 2017-02-16 12:00
    关注

    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 {
    
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测