如何检查请求是否是通过 CodeIgniter 中的 AJAX 发出的? [英] How do I check if the request is made via AJAX in CodeIgniter?

查看:26
本文介绍了如何检查请求是否是通过 CodeIgniter 中的 AJAX 发出的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查请求是否为 AJAX?我正在使用 CodeIgniter.我有一个链接,当它点击时,它会打开弹出对话框窗口,这是通过 ajax 完成的,它请求控制器名称 login_window().

How do I check if the request is an AJAX? I am using CodeIgniter. I have a link that when it clicked, it'll open the pop-up dialog window this is done through ajax it requests to a controller name login_window().

//Here is the controller name:
function login_window(){
    // request via ajax
    $this->load->view("login_window");
}

jQuery

//here is the jquery code:
//I am using a jquery plugin FACEBOX

$('a[rel*=dialog]').facebox();

<a href="http://localhost/codeigniter/login_window" rel="dialog">Login</a>

我想检查它是否是 AJAX 请求,如果不是,我会将它们重定向到主页.所以他们无法访问仅用于 ajax 请求的页面.

I want to check if it is an AJAX request and if not, i will redirect them to homepage. so there's no way they can access the page that is intended only for ajax requests.

推荐答案

如果您使用的是发送 X-Requested-With 标头的库,那么您可以执行...

If you are using a library that sends the X-Requested-With header, then you can do...

if (strtolower(filter_input(INPUT_SERVER, 'HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest') {
   // I'm AJAX!
}

这篇关于如何检查请求是否是通过 CodeIgniter 中的 AJAX 发出的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆