has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

This appears to be a cross-domain problem, I appeared in the process of vue using axios to send a request directly to the java background

Then the code I solved in the backend is as follows plus the following code

@Configuration
 public  class WebConfig implements WebMvcConfigurer { 


    @Override 
    public  void addCorsMappings (CorsRegistry registry) {
         // Set the path that allows cross-domain 
            registration.addMapping (" 
        / **" )
             // Set the domain name that allows cross-domain requests.allowedOrigins ("* " )
             // Whether certificates are allowed. 
            AllowCredentials ( true ) 
            .allowedMethods ( " * " ) 
            .maxAge ( 3600 ); 
    } 

        ... 
}

 

Guess you like

Origin www.cnblogs.com/zzl521/p/12702018.html