-1

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?

4
  • 2
    Where did you define is_ajax? Commented Oct 23, 2012 at 10:09
  • 2
    How do you expect $_REQUEST to contain is_ajax? Commented Oct 23, 2012 at 10:09
  • 1
    Oh my dear, I forgot to include is_ajax: 1 in data being sent. Too many hours in code right now... Commented Oct 23, 2012 at 10:11
  • you want to read this[api.jquery.com/jQuery.ajax/] ,here many samples, for used Ajax with Jquery, I thinks, Lex, and Muthu are the response, you want to define is_ajax value, in your Ajax's request... Commented Oct 23, 2012 at 10:15

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

I think @Florent should repost this for the answer
2

try this :

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

Comments

1

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';
}

1 Comment

Who is setting the HTTP header? I wouldn't trust IE8 to insert this header.
1

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);
             }
        });

Comments

1

Try this :

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

4 Comments

@jan dvorak : y dont you answer then instead of commenting on every answer !!
@JanDvorak sufficient on what? they using more complex way. _POST is the standard way to receive on Ajax method.
@memosdp OP was using $_REQUEST, which is a valid way to access POST variables. OP was missing the POST variable he was testing. This answer does not add the variable, only tests it in a different way./
@JanDvorak i misunderstood that you said about sufficiency ... it was for the sufficiency like an answer ... took me a while to realize it ... despite this not worths a downvote ... as you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.