PHP无法识别AJAX调用

I'm calling a file via jQuery's AJAX method looks like this:

    $is_ajax = $_REQUEST['is_ajax'];
    if(isset($is_ajax) && $is_ajax)
    {
        echo 'its ajax!';
    }
    else echo 'im called, but not via ajax';

and my AJAX call:

jQuery.ajax({
                type: 'POST',
                url: 'my-service.php',
                data: data,
                success: function(response) { 
                    alert(response);
                 }
            });

But alert always shows 'not ajax' message. What am I doing wrong?

Another method

You can simple check whether the request is AJAX or not like this,

/* AJAX check  */
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  echo 'its ajax!';
}else{
  echo 'im called, but not via ajax';
}

Needed to add is_ajax: 1 in data that is being sent via AJAX call.

try this :

jQuery.ajax({
            type: 'POST',
            url: 'my-service.php',
            data: {is_ajax:1},
            success: function(response) { 
                alert(response);
             }
        });

edit your AJAX call and modify the data

jQuery.ajax({
            type: 'POST',
            url: 'my-service.php',
            data: {"is_ajax":"the value"},
            success: function(response) { 
                alert(response);
             }
        });

Try this :

if(isset($_POST['is_ajax']))
{
    echo 'its ajax!';
}
else 
{
echo 'im called, but not via ajax';
}

It seems that your method of detecting AJAX is wrong. If your server supports HTTP_X_REQUESTED_WITH, then it's the way to go :

if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  /* special ajax here */
  die($content);
}

source : Detect an AJAX Request