Geln Yang
Geln Yang

Reputation: 912

How to know a HTTP request is from Ajax?

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

Upvotes: 10

Views: 1646

Answers (3)

Migol
Migol

Reputation: 8448

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

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532465

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.

Upvotes: 11

Mark Redman
Mark Redman

Reputation: 24515

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?

Upvotes: 0

Related Questions