简体   繁体   English

对预检请求的响应未通过访问控制检查:不存在“Access-Control-Allow-Origin”标头

[英]Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present

I Am not able to fetch data from Rest Server.Following Error is coming: "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost:8080 ' is therefore not allowed access."我无法从 Rest Server 获取数据。出现以下错误:“对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。来源“ http ://localhost:8080 ' 因此不允许访问。”

Response is coming as "0", This is not hitting the Rest Method also.响应为“0”,这也没有达到休息方法。

Rest APT:休息 APT:

@POST
    @Timed
    @Path("updateScore")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response updateScore(Player player)
    {
    StringBuilder returnStr = new StringBuilder("Sucess");
    return Response.ok().
            header("Access-Control-Allow-Origin", "http://localhost:8080").
            header("Access-Control-Allow-Methods",  "GET, POST, PATCH, PUT, DELETE, OPTIONS").
            header("Access-Control-Allow-Headers", "Origin, Content-Type, X-Auth-Token").
            allow("OPTIONS").
            status(200).
            entity("Hello").build();
}

JavaScript Call

    var url = "http://192.168.0.101:9090/api/FGame/updateScore/";
            var client = new XMLHttpRequest();
            client.open('POST', url, true);
            client.setRequestHeader('Content-Type', 'application/json');
            client.send('{"Name" : 12}');
            client.onreadystatechange = function() {
                   alert(client.status);
                   alert(client.data)
            };

But if I am changing to JavaScript call as following then working fine.但是,如果我按照以下方式更改为 JavaScript 调用,那么工作正常。 JavaScript Call JavaScript 调用

    var url = "http://192.168.0.101:9090/api/FGame/updateScore/";
            var client = new XMLHttpRequest();
            client.open('POST', url, true);
            client.send(null);
            client.onreadystatechange = function() {
                   alert(client.status);
                   alert(client.data)
            };

In my scenario I am using an angular front-end Api to collect the data from a form object and a Java rest Api to write the data to a MySql database.在我的场景中,我使用 Angular 前端 Api 从表单对象收集数据,并使用 Java rest Api 将数据写入 MySql 数据库。 But I was always having the "Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource."但我总是遇到“对预检请求的响应未通过访问控制检查:请求的资源上不存在‘Access-Control-Allow-Origin’标头。” error in the browser console.浏览器控制台中的错误。 None of the suggested header modifications in the frontend like;前端中建议的标题修改都不像;

const headers: {
  'Access-Control-Allow-Origin' : '*',
};

worked for me.为我工作。

After hours of research in the internet one of my friends showed me the answer in not in the frontend but in the backend.在互联网上进行了数小时的研究后,我的一位朋友向我展示了答案,不是在前端,而是在后端。 Adding a WebConfig class to my java code fixed my problem.在我的 java 代码中添加一个 WebConfig 类解决了我的问题。

@EnableWebMvc
@Configuration
public class WebConfig implements WebMvcConfigurer {

 @Override
 public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**")
            .allowedOrigins("http://localhost:3000/", "http://localhost:4200/")
            .allowedMethods("*")
            .allowedHeaders("*");
 }
}

Hope this finds those who is looking for an answer to the same error.希望这能找到那些正在寻找相同错误答案的人。

暂无
暂无

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

相关问题 AWS-对预检请求的响应未通过访问控制检查:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - AWS - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource 对预检请求的响应未通过访问控制检查:Angular js中没有“ Access-Control-Allow-Origin”标头 - Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present in angular js "Angular 7:对预检请求的响应未通过访问控制检查:请求中不存在“Access-Control-Allow-Origin”标头" - Angular 7 : Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested 获取请求不会通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头 - Getting request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 错误:对预检请求的响应 'Access-Control-Allow-Origin' header 包含多个值 '*、*'、 - CORS error: Response to preflight request The 'Access-Control-Allow-Origin' header contains multiple values '*, *', React+Express:预检响应中的 Access-Control-Allow-Headers 不允许请求标头字段 access-control-allow-origin - React+Express: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response “请求 header 字段 Access-Control-Allow-Origin 在预检响应中被 Access-Control-Allow-Headers 不允许”尽管 CORS 配置有效 - “Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response” despite valid CORS config 不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present AJAX请求中不存在“ access-control-allow-origin”标头 - No 'access-control-allow-origin' header is present in AJAX request AJAX请求中没有“ Access-Control-Allow-Origin”标头存在错误 - No 'Access-Control-Allow-Origin' header is present error in AJAX Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM