【问题标题】:JSP/Servlet How to identify if the HTTP request came from an ajax requestJSP/Servlet 如何识别 HTTP 请求是否来自 ajax 请求
【发布时间】:2012-12-09 21:41:21
【问题描述】:

我正在为我的网络应用程序使用 JQuery-Ajax 和 History.js,我是否可以确定该请求是来自 AJAX 请求还是只是普通请求?我的目标是这样的

<%if(isRequestAjax()){%>
<jsp:include page="/views/header.jsp" />        
}
<%}%>

<div id ="profilePage>
    <!-- user profile will be loaded here !-->
</div>

我也使用 Struts2 和 MVC 框架

【问题讨论】:

    标签: java jsp servlets struts2 request


    【解决方案1】:

    您必须检查请求标头X-Requested-With,它将是XMLHttpRequest

      if ("XMLHttpRequest".equals(req.getHeader("X-Requested-With")) ){
               // include other file
      } 
    

    【讨论】:

    • 怎么样?你似乎有点离散,一个明确的例子会很好
    • 不,不是。我不确定此标头是否由所有浏览器/用户代理设置。所以我会在 request 中传递一个额外的参数来按请求分类来识别。
    猜你喜欢
    • 2014-11-11
    • 2012-01-03
    • 2013-01-28
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多