如何区分 Ajax 请求和普通 Http 请求? [英] How to differentiate Ajax requests from normal Http requests?
问题描述
我在我的应用程序中使用 JSF 框架.我需要在 Phase Listener 类中的渲染响应阶段之前运行特定脚本.
I am using JSF framework in my application. I need to run a specific script before the render response phase in my Phase Listener class.
运行此脚本的条件是,如果触发的请求是 Ajax 请求,我需要运行该脚本,如果触发的请求是 Http 请求,我不应该运行该脚本.
Condition for running this script is that, if the request triggered is a Ajax request i need to run the script, if the request triggered is a Http request i should not run that script.
谁能帮我区分收到的请求.
Can anyone please help me to differentiate the requests recieved.?
推荐答案
Ajax 请求通常有一个 X-Requested-With: XMLHttpRequest
请求头.在 JSF 中,您可以通过 ExternalContext#getRequestHeaderMap()
.
Ajax requests have usually a X-Requested-With: XMLHttpRequest
request header. In JSF, you can obtain the request headers by ExternalContext#getRequestHeaderMap()
.
ExternalContext externalContext = facesContext.getExternalContext();
Map<String, String> headers = externalContext.getRequestHeaderMap();
boolean ajax = "XMLHttpRequest".equals(headers.get("X-Requested-With"));
这篇关于如何区分 Ajax 请求和普通 Http 请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!