JSP/Servlet如何识别HTTP请求是否来自ajax请求 [英] JSP/Servlet How to identify if the HTTP request came from an ajax request

查看:64
本文介绍了JSP/Servlet如何识别HTTP请求是否来自ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将JQuery-Ajax和History.js用于我的Web应用程序,无论如何,我是否可以识别该请求是来自AJAX请求还是仅来自普通请求?我的目标是像这样

I am using JQuery-Ajax, and History.js for my web app, is there anyway I can identify if the request came from an AJAX request or just normal request? What I aim to do is something like this

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

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

我也在MVC框架中使用Struts2

I am also using Struts2 as may MVC framework

推荐答案

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

You have to check for a request header X-Requested-With it will be XMLHttpRequest

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

这篇关于JSP/Servlet如何识别HTTP请求是否来自ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆