(跨域问题)Access to XMLHttpRequest at xxx from origin xxx

问题:Access to XMLHttpRequest at ‘http://localhost:8181/book/findAll’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.
解决方法:前端后端都可解决
①springboot后端:
创建配置类CorsConfig

@Configuration
public class CrosConfig implements WebMvcConfigurer {
    
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
    
    
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET","POST","HEAD","PUT","DELETE")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

猜你喜欢

转载自blog.csdn.net/CSDN13822805069/article/details/123292539