react-admin No'Access-Control-Allow-Origin'

问题描述 投票:1回答:1

我正在我的项目中尝试使用react-admin,这需要Content-Range

所以我在服务器上写了:

@GetMapping("admin/user")
    ResponseEntity<List<User> > getAll()
    {
        System.out.println(new Date());;

        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.set("Content-Range",
                "posts 0-"+userRepository.findAll().size()+"/"+userRepository.findAll().size());

        return ResponseEntity.ok()
                .headers(responseHeaders)
                .body(userRepository.findAll());
    }

也已配置的CORS:

@Bean
    public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry
                        .addMapping("/**")
                        .allowedOrigins("http://localhost:3000");
                registry
                        .addMapping("/admin*")
                        .allowedOrigins("http://localhost:3001")
                        .exposedHeaders("Content-Range");

            }
        };
    }

错误:从原点“ http://localhost:8080/admin/user?filter=%7B%7D&range=%5B0%2C9%5D&sort=%5B%22id%22%2C%22ASC%22%5D”到“ http://localhost:3001”的访存访问已被CORS策略阻止:所请求的资源上没有“ Access-Control-Allow-Origin”标头。如果不透明的响应满足您的需求,请将请求的模式设置为“ no-cors”,以在禁用CORS的情况下获取资源。

为什么会出现此错误?

在邮递员上工作良好:

enter image description here

enter image description here

后来我甚至添加了responseHeaders.set("Origin", "http://localhost:3001");

仍然没有希望。

如何解决?

reactjs spring spring-boot cors react-admin
1个回答
1
投票

这是由浏览器引起的cros错误。

© www.soinside.com 2019 - 2024. All rights reserved.