11

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?

Graviton
  • 81,782
  • 146
  • 424
  • 602

6 Answers6

12

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";
}
karim79
  • 339,989
  • 67
  • 413
  • 406
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;
Anthony
  • 36,459
  • 25
  • 97
  • 163
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']

kristian nissen
  • 2,809
  • 5
  • 44
  • 68
  • 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. – Havenard Sep 04 '09 at 06:42
  • 1
    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 – PHP Avenger Jan 27 '15 at 17:56
  • 1
    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` – PHP Avenger Jan 27 '15 at 18:00
4

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

$isAjaxRequest = $_SERVER['X_REQUESTED_WITH'] == 'XMLHttpRequest';
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 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? – Justin May 07 '13 at 20:02
-1

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

AriehGlazer
  • 2,220
  • 8
  • 26
  • 29
  • 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 https://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 – Gerben Versluis Jan 05 '23 at 23:16
-2

Try json_decode()

Deniss Kozlovs
  • 4,761
  • 2
  • 28
  • 35