By ganeshread
via gmarwaha.com
Published: Jun 28 2009 / 17:28
In modern web application development, many a times you would want to know if the incoming HTTP Request is an AJAX request or just a Normal request. Have you come across this requirement? I have, and the solution that I found turned out to be pretty straight-forward and I will be sharing it with you here.
Comments
Bob Schellink replied ago:
I don't think the X-Requested-With header is set for Ajax requests automatically. However JavaScript libraries such as jQuery, Prototype, Mootools etc do set this header so its probably a non issue unless you code Ajax by hand.
John Rockefeller replied ago:
I also like the comments section which provides an alternate solution.
Ganeshji Marwaha replied ago:
@sabob: Yes, you are right. I forgot to mention it in the post. Will update the post with necessary details.
Johan Vos replied ago:
It is indeed very useful to detect whether a request comes from an Ajax call or from a plain html request.
This question is exactly something that can be answered using Jersey (JSR 311 refimpl) by using annotations (e.g. @Produces("application/json").
See my blog entry at http://blogs.lodgon.com/johan/Glassfish_REST_and_Jersey
overtheline.myopenid.com replied ago:
&imajax=true
God
Voters For This Link (20)
Voters Against This Link (1)