Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know a HTTP request is from Ajax?

Tags:

ajax

http

request

Is it possible to know that a HTTP request is from Ajax?If yes, how?

like image 482
Geln Yang Avatar asked Feb 04 '23 05:02

Geln Yang


1 Answers

Many frameworks add a header X-Requested-With set to XMLHttpRequest when sending an AJAX request. If you are using jQuery or Microsoft frameworks, this should work. If using another framework, you'll have to check the documentation. Since normal requests don't have the header, a check for the presence of the header should be sufficient.

If you are using your own "home-built" AJAX or the framework doesn't do this, but does allow you to set a header, you could simply follow this convention and add your own header when making the request.

like image 131
tvanfosson Avatar answered Feb 23 '23 07:02

tvanfosson