[英]How to find out if a request is an ajax request?
I try to find out if a request to a PHP file is sent by ajax or not. 我试图找出ajax是否发送了对PHP文件的请求。
I googled it and read a whole a bunch of articles that suggest following method: 我用Google搜索并阅读了一大堆提示以下方法的文章:
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
echo 'This is an ajax request!';
exit;
}
echo 'This is not an ajax request!';
But my server doesn't have this variable: Undefined index: HTTP_X_REQUESTED_WITH ...
但我的服务器没有这个变量:
Undefined index: HTTP_X_REQUESTED_WITH ...
Thats how I make the ajax request: 多数民众赞成我如何制作ajax请求:
$.ajax({
url: 'http://URL/test.php',
complete: function(res) {
console.log(res.responseText);
}
});
I'm making that call from a different url, so I've set header('Access-Control-Allow-Origin: *');
我正在从另一个网址拨打电话,所以我设置了
header('Access-Control-Allow-Origin: *');
I have discovered one difference in $_SERVER
though: 我在
$_SERVER
发现了一个区别:
Ajax request: $_SERVER[HTTP_ACCEPT] => */*
Ajax请求:
$_SERVER[HTTP_ACCEPT] => */*
No Ajax request: $_SERVER[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
没有Ajax请求:
$_SERVER[HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
So my question is, is there a way for me to get HTTP_X_REQUESTED_WITH
into $_SERVER
? 所以我的问题是,有没有办法让
HTTP_X_REQUESTED_WITH
进入$_SERVER
? And if not, is there a proper way to find out if the request is AJAX by using the $_SERVER[HTTP_ACCEPT]
如果没有,是否有正确的方法通过使用
$_SERVER[HTTP_ACCEPT]
来确定请求是否为AJAX
There's no 100% way to detect if the request was made via ajax. 没有100%的方法来检测请求是否是通过ajax进行的。 Even if someone sends header with "X-Requested-With: XMLHttpRequest" you shouldn't rely on it.
即使有人发送标题为“X-Requested-With:XMLHttpRequest”,你也不应该依赖它。
what you can do is provide your own defined variable, and use a command design pattern to test the outcome for instance: 您可以做的是提供您自己定义的变量,并使用命令设计模式来测试结果,例如:
$.ajax({
url: 'http://URL/test.php',
data: {action: "ajax_request"},
complete: function(res) {
console.log(res.responseText);
}
});
and the php test: 和PHP测试:
if (isset($_POST['action']) && !empty($_POST['action'])) {
$action = $_POST['action'];
switch ($action) {
case 'ajax_request' : echo 'This is an ajax request!';
break;
}
}
else
echo 'This is not an ajax request!';
try this also 试试这个
while(true)
{
......
if (window.XMLHttpRequest){
echo 'This is an ajax request!';
return new XMLHttpRequest();
}
else if(window.ActiveXObject)// for internet explorer
{
echo 'This is an ajax request'!;
return new ActiveXObject("Microsoft.XMLHTTP");
}
else
echo 'This is not an ajax request!';
}
Not all browsers will send that response I usually use 并非所有浏览器都会发送我通常使用的响应
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest') {//Do stuff }
and for ajax 并为ajax
request = $.ajax({
url: SomePage.php,
type: "POST",
data: {key: value}
});
request.done(function(returnedData) {
//do done stuff
});
request.fail(function(jqXHR, textStatus) {
//do fail stuff
});
Note: 注意:
$HTTP_SERVER_VARS contains the same initial information, but is not a superglobal.
(Note that $HTTP_SERVER_VARS and $_SERVER are different variables and that
PHP handles them as such). Also note that long arrays were removed since PHP 5.4.0 so
$HTTP_SERVER_VARS doesn't exist anymore.
So var_dump($HTTP_SERVER_VARS);
所以
var_dump($HTTP_SERVER_VARS);
to see if its contained in there, also note that the $_SERVER
is filled in by the webserver 要查看它是否包含在那里,还要注意
$_SERVER
是由网络服务器填写的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.