• 如何解决跨域问题,以及跨域预检未通过问题


    has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.

    最近在前后端联调时,当我通过axios在前端进行POST请求时,浏览器报如下错误:

    has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.
    

    乍一看是跨域问题,但其实并不是跨域问题。由于浏览器将CORS请求分为两类:简单请求(simple request)和非简单请求(not-simple-request)。非简单请求 会在正式通信之前,增加一次HTTP请求,称之为预检请求。浏览器会先发起OPTIONS方法到服务器,以获知服务器是否允许该实际请求。

    这是在第一次预请求时候没有请求成功,预请求不成功。

    先聊一下在SpringBoot项目中如何处理跨域问题:添加如下代码:

    @Component
    public class SimpleCORSFilter implements Filter {
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD,PUT");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
            chain.doFilter(req, res);
        }
    
        @Override
        public void init(FilterConfig filterConfig) {}
    
        @Override
        public void destroy() {}
    
    }
    

    其次如何解决has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: It does not have HTTP ok status.这个错误

    @Component
    public class SimpleCORSFilter implements Filter {
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
            HttpServletResponse response = (HttpServletResponse) res;
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, HEAD,PUT");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "access-control-allow-origin, authority, content-type, version-info, X-Requested-With");
            HttpServletRequest request = (HttpServletRequest)req;
            if ("OPTIONS".equals(request.getMethod())) {
                response.setStatus(HttpServletResponse.SC_OK);
                return;
            }
    
            chain.doFilter(req, res);
        }
    
        @Override
        public void init(FilterConfig filterConfig) {}
    
        @Override
        public void destroy() {}
    
    }
    
  • 相关阅读:
    新浪开放平台入门及基本步骤(java SDK)
    Android 解析JSON数据示例代码[CODE]
    Ubuntu10.04 搭建android平台
    程序员真的很懒
    android之ContentResolver与ContentProvider
    写代码如写散文
    通过OAuth认证和操作新浪微博
    喜报:凌阳教育深圳中心正式成立
    25个让Java程序员更高效的Eclipse插件
    Java Json开源解析包 googlegson download(下载)
  • 原文地址:https://www.cnblogs.com/gaolight/p/14446100.html
Copyright © 2020-2023  润新知