简体   繁体   English

反应管理员没有“访问控制允许来源”

[英]react-admin No 'Access-Control-Allow-Origin'

I'm trying to use react-admin in my project, which requires Content-Range .我正在尝试在我的项目中使用react-admin ,这需要Content-Range

So in my server I have written :所以在我的服务器中我写了:

@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());
    }

Also configured CORS :还配置了 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");

            }
        };
    }

Error : Access to fetch at ' http://localhost:8080/admin/user?filter=%7B%7D&range=%5B0%2C9%5D&sort=%5B%22id%22%2C%22ASC%22%5D ' from origin ' http://localhost:3001 ' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.错误:访问在“ 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”标头。 If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.如果不透明响应满足您的需求,请将请求的模式设置为“no-cors”以在禁用 CORS 的情况下获取资源。

Why am I getting this error ?为什么我收到这个错误?

Working fine on postman:邮递员工作正常:

在此处输入图片说明

在此处输入图片说明

Later I even added : responseHeaders.set("Origin", "http://localhost:3001");后来我什至补充说: responseHeaders.set("Origin", "http://localhost:3001");

Still no hope.还是没有希望。

How this can be resolved ?如何解决?

This is cros error causing by browser.这是浏览器导致的 cro 错误。

You can configure your proxy with your react app package.json file just add您可以使用您的反应应用程序 package.json 文件配置您的代理,只需添加

"proxy" : "http://localhost:8080"  

Now you just need to provide relative path to your api request现在您只需要提供 api 请求的相对路径

 Example : '/admin/user/'  // your app going to make request to http://localhost:8080/admin/user 

And also make sure you are allowing right url of your back-end并确保您允许后端的正确网址

                  registry
                    .addMapping("/**")
                    .allowedOrigins("http://localhost:3000");   // make sure this is right url of your frontend 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM