Access to XMLHttpRequest has been blocked by CORS policy

后端 未结 7 1738
-上瘾入骨i
-上瘾入骨i 2021-02-18 16:58

I\'ve a problem when I try to do PATCH request in an angular 7 web application. In my backend I have:

app.use((re         


        
7条回答
  •  一整个雨季
    2021-02-18 17:09

    If you are using Tomcat try this: full documentation

     HttpServletResponse response = (HttpServletResponse) servletResponse;
            HttpServletRequest request = (HttpServletRequest) servletRequest;
    
            response.setHeader("Access-Control-Allow-Origin", "*");
            response.setHeader("Access-Control-Allow-Credentials", "true");
            response.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, PUT, DELETE, OPTIONS, HEAD");
            response.setHeader("Access-Control-Allow-Headers", "Origin, Accept, X-Requested-With, Content-Type, "
                    + "Access-Control-Request-Method, Access-Control-Request-Headers, Authorization");
    
    
            if ( request.getMethod().equals("OPTIONS") ) {
                response.setStatus(HttpServletResponse.SC_OK);
                return;
            }
    
    
            filterChain.doFilter(servletRequest, servletResponse);
    

    Then in web.xml

    
        cors
        classYouImplementTheCodeAbove
    
    
    
        cors
        /*
    
    

    If you are using other go to https://enable-cors.org/server.html and search for it

提交回复
热议问题