Java中判断是否Ajax异步请求

Java 中判断是否 Ajax 异步请求代码如下:

/**
 * 是否是Ajax异步请求
 *
 * @param request
 */
public static boolean isAjaxRequest(HttpServletRequest request) {
    String accept = request.getHeader("accept");
    if (accept != null && accept.indexOf("application/json") != -1) {
        return true;
    }

    String xRequestedWith = request.getHeader("X-Requested-With");
    if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1) {
        return true;
    }

    String uri = request.getRequestURI();
    if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
        return true;
    }

    String ajax = request.getParameter("__ajax");
    if (StringUtils.inStringIgnoreCase(ajax, "json", "xml")) {
        return true;
    }
    return false;
}

是,返回:true,不是则返回:false

如您在阅读中发现不足,欢迎留言!!!

原创文章 107 获赞 650 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_40065776/article/details/105977836
今日推荐