CORS跨域请求405错误的另一个坑

巨坑:

和同事做联调(我后端,他前端)解决完跨域请求403错误,又冒出来一个405错误,查了近一个小时,最后发现是他请求的URL错了!发现post跨域请求原本404错误的,会因为是跨域而报405错误,让人脑袋疼!!!

关于解决CORS跨域请求403错误,filter方法:

@Component
public class CorsAllowFilter extends OncePerRequestFilter {

    private static boolean isStaticResources( HttpServletRequest req ) {
        String uri = req.getRequestURI();
        return uri.endsWith(".ico") || uri.endsWith(".html");
    }
    @Override
    protected void doFilterInternal( HttpServletRequest req, HttpServletResponse resp, FilterChain chain ) throws ServletException, IOException {
        if (isStaticResources(req)) {
            chain.doFilter(req, resp);
            return;
        }
        resp.setHeader("Content-type", "application:json;charset=utf8");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        resp.setHeader("Access-Control-Allow-Methods", "PUT,GET,POST,DELETE,OPTIONS");
        resp.setHeader("Access-Control-Allow-Headers", "content-type,x-auth");
        chain.doFilter(req, resp);
    }

}

关于405错误参照:https://blog.csdn.net/qq_35804654/article/details/77272304

 

你可能感兴趣的:(CORS跨域请求405错误的另一个坑)