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才可以)
  • 相关阅读:
    获取Mysql-jdbc驱动Driver类的两种方式
    Misha and Changing Handles
    What Are You Talking About (map)
    Let the Balloon Rise <map>的应用
    ignitius and princess 2(全排列)
    大一下学期计划
    大一上学期总结
    algorithm的基本注意事项
    STL的注意事项
    STL的基本操作指令
  • 原文地址:https://www.cnblogs.com/pxblog/p/15683301.html
Copyright © 2011-2022 走看看