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?
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?
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;
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']
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.8You can check the X-Requested-With header, some libraries, like jQuery set it to "XMLHttpRequest".
$isAjaxRequest = $_SERVER['X_REQUESTED_WITH'] == 'XMLHttpRequest';
$is_ajax = ($_SERVER['X-Requested-With'] == 'XMLHttpRequest'); instead?you can always set an extra header specifying that, or use an arbitrary variable to indicate JSON requests.