我如何检查,如果请求在codeIgniter通过AJAX做? [英] How do I check if the request is made via AJAX in CodeIgniter?
本文介绍了我如何检查,如果请求在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-要求 - 以
头一个图书馆,那么你可以做......
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屋!
查看全文