I try to find out if a request to a PHP file is sent by ajax or not.
I googled it and read a whole a bunch of articles that suggest following method:
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:
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!';
}