开发者

How to know a HTTP request is from Ajax?

开发者 https://www.devze.com 2022-12-26 02:48 出处:网络
Is it possible to know that 开发者_如何转开发a HTTP request is from Ajax?If yes, how?Many frameworks add a header X-Requested-With set to XMLHttpRequest when sending an AJAX request.If you are using j

Is it possible to know that 开发者_如何转开发a HTTP request is from Ajax?If yes, how?


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.


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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消