PHP check whether Incoming Request is JSON type

前端 未结 6 1091
轻奢々
轻奢々 2020-12-20 14:57

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

I tried

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

6条回答
  •  失恋的感觉
    2020-12-20 15:37

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

提交回复
热议问题