zoukankan      html  css  js  c++  java
  • 如何解决跨域问题,以及跨域预检未通过问题

    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() {}
    
    }
    
  • 相关阅读:
    Ubuntu 16 安装redis客户端
    crontab 参数详解
    PHP模拟登录发送闪存
    Nginx配置端口访问的网站
    Linux 增加对外开放的端口
    Linux 实用指令之查看端口开启情况
    无敌的极路由
    不同的域名可以指向同一个项目
    MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error
    Redis 创建多个端口
  • 原文地址:https://www.cnblogs.com/gaolight/p/14446100.html
Copyright © 2011-2022 走看看