http://zwllxs.iteye.com/blog/1070429
在一次做非常复杂的ajax应用时,如果一个会话已经超时,但是此时再通过ajax请求,那么ajax返回的则是一个登陆页面的html,那这下就惨了,页面上而已就乱了,那么,能否在java端,如拦截器里判断客户的的请求是否是ajax请求呢,经过查询,能.
普通请求与ajax请求的报文头不一样,通过如下
- String requestType = request.getHeader("X-Requested-With");
如果requestType能拿到值,并且值为XMLHttpRequest,表示客户端的请求为异步请求,那自然是ajax请求了,反之如果为null,则是普通的请求
https://www.oschina.net/code/snippet_19410_36902
/**
* 判断ajax请求
* @param request
* @return
*/
boolean
isAjax(HttpServletRequest request){
return
(request.getHeader(
"X-Requested-With"
) !=
null
&&
"XMLHttpRequest"
.equals( request.getHeader(
"X-Requested-With"
).toString()) ) ;
}
http://www.cnblogs.com/zhuzhiq/p/5585030.html
判断是否Ajax请求
public static final String AJAX_ACCEPT_CONTENT_TYPE = "text/html;type=ajax";
public static final String AJAX_SOURCE_PARAM = "ajaxSource";
protected boolean isAjaxRequestInternal(HttpServletRequest request, HttpServletResponse response) {
String acceptHeader = request.getHeader("Accept");
String ajaxParam = request.getParameter(AJAX_SOURCE_PARAM);
if (AJAX_ACCEPT_CONTENT_TYPE.equals(acceptHeader) || StringUtils.hasText(ajaxParam)) {
return true;
} else {
return false;
}
}