Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP check whether Incoming Request is JSON type

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?

like image 455
Graviton Avatar asked Sep 04 '09 06:09

Graviton


2 Answers

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";
}
like image 159
karim79 Avatar answered Oct 14 '22 10:10

karim79


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']

like image 33
kristian nissen Avatar answered Oct 14 '22 10:10

kristian nissen