本文共 512 字,大约阅读时间需要 1 分钟。
我们有时候需要根据请求类型来判断返回视图名称还是JSON数据,这里记录一个判断Ajax的工具类方便日后好找
通过传入
Request
对象获取头信息,根据头信息判断是否属于Ajax请求
public class AjaxUtil { /** * 用来判断请求属不属于Ajax请求 * @param req http请求 * @return true表示ajax请求 */ public static boolean isAjax(HttpServletRequest req){ String accept = req.getHeader("Accept"); String with = req.getHeader("X-Requested-With"); return ( accept != null && accept.contains("application/json") ) || ( with != null && with.contains("XMLHttpRequest") ); }}
转载地址:http://rtxuz.baihongyu.com/