zoukankan      html  css  js  c++  java
  • 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 (isContainStrs(uri, ".json", ".xml")) {
                return true;
            }
    
            String ajax = request.getParameter("__ajax");
            if (isContainStrs(ajax, "json", "xml")) {
                return true;
            }
            return false;
        }
    
    
        public static boolean isContainStrs(String str, String... strs) {
            if (str != null && strs != null) {
                for (String s : strs) {
                    if (str.equalsIgnoreCase(trim(s))) {
                        return true;
                    }
                }
            }
            return false;
        }
    
      
        public static String trim(String str) {
            return (str == null ? "" : str.trim());
        }
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    打印出乘法表
    python小练习1
    JavaScript 两个变量互换
    纯css3 画一个小猪佩奇
    箭头函数
    点击获取li下标的几种方式
    再见!!!!!!
    十月一前期
    大家好!!!!!!
    雕刻技
  • 原文地址:https://www.cnblogs.com/pxblog/p/15683301.html
Copyright © 2011-2022 走看看