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

时间:2016-10-31 12:51:13

标签: javascript java rest jetty dropwizard

我无法从Rest Server获取数据。以下错误即将发生: “对预检请求的响应未通过访问控制检查:请求的资源上没有'Access-Control-Allow-Origin'标头。因此不允许原点'http://localhost:8080'访问。”

响应变为“0”,这也没有达到Rest方法。

休息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)
            };

但是,如果我改变为以下JavaScript调用,那么工作正常。 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)
            };

0 个答案:

没有答案