Ajax请求中主要对象(原生对象)是XMLHttpRequest,知道了该对象,那么就可以通过判断请求头属性来鉴别当前请求。
//判断当前请求是否为Ajax
public static boolean isAjaxRequest(HttpServletRequest request) {
String header = request.getHeader("X-Requested-With");
return !StringUtils.isEmpty(header) && "XMLHttpRequest".equals(header);
}