10

Is there anyway to check whether an incoming request is of AJAX JSON type?

I tried

if(($_SERVER['REQUEST_METHOD']=='JSON'))
{
}

But it didn't work.

Any thoughts?

2

6 Answers 6

13

You would need to set a header from the client side. jQuery and other libraries set a x-requested-with header:

if(strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
   echo "Ajax request";
}
Sign up to request clarification or add additional context in comments.

Comments

12

Where are you accepting requests from, exactly, that you wouldn't know?

You could have a function at the beginning of the script that tries to import the data as JSON or simplexml. If it catches an error, you know it's the other one...

On second thought, have it test it to be JSON, simplexml will throw an error for tons of reasons.

 $json_request = (json_decode($request) != NULL) ? true : false;

Comments

4

You can do a check on the accept param, if it's text/javascript your talking json, if it's text/xml guess what :P

$_SERVER['HTTP_ACCEPT']

3 Comments

You are assuming that the JavaScript program will be setting the XMLHttpRequest's headers correctly, which is never done (who cares?). Generally the server-side script is made to answer in only one format, and I can't see any reason why it should be different.
It is a standard procedure when you are request some page or service (hitting a URL), you should specify what type of result you are expecting in the request. I have worked on many web services where user expect information in XML or JSON so they specify that in request param. other wise they have to provide extension of the of URL let say somesite.com/get_user[.xml | .json]. Many liberalises like Jersey etc in Java, jQuery and few php custom curl libraries do append this information in header of the request (Standard way of communication). otherwise pass extra param in each request
When browser intitate the communcation with any site's service (or URL), They do specify text/html, jQuery do set application/json in request header if dataType is set to json like dataType: 'json' and above variable is as follows application/json, text/javascript, */*; q=0.01 browser opened URL will show text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
4

You can check the X-Requested-With header, some libraries, like jQuery set it to "XMLHttpRequest".

$isAjaxRequest = $_SERVER['X_REQUESTED_WITH'] == 'XMLHttpRequest';

1 Comment

In your link, jQuery uses 'X-Requested-With' not 'X_REQUESTED_WITH'. So wouldn't we do $is_ajax = ($_SERVER['X-Requested-With'] == 'XMLHttpRequest'); instead?
-1

you can always set an extra header specifying that, or use an arbitrary variable to indicate JSON requests.

1 Comment

Thank you for answering. This will work. However there are existing headers meant exactly for this. i.e. 'accept' where you can define which kind of types you will accept as a return value. More on this can be found at developer.mozilla.org/en-US/docs/Glossary/Request_header . As a server you can either define a protocol, parse the request as they want it to be returned, detect the input, or build specific urls per protocol
-2

Try json_decode()

Comments

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.