tags:

views:

67

answers:

4

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

A: 

I don't think it is.

hamax
A: 

I would assume that any request received by a server would appear to be the same (ie http post/get) and that you would need to look at the referer, but that may just give you the browser details?

Mark Redman
+7  A: 

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.

tvanfosson
+1  A: 

Most frameworks set X-Requested-With header to state it. But standard AJAX requests doesn't.

Migol