无法让 Access-Control-Allow-Origin header 像我预期的那样工作
Can't get Access-Control-Allow-Origin header to work as I expected
关于这个问题有很多问题,但我似乎仍然无法解决我的问题。
我正在尝试使用 HTML 5 Chrome 中的一款游戏。 Link here.
该游戏是使用 libgdx 编写的,我从我的应用引擎托管后端发布 json 数据。我读了很多书,我想我理解跨域访问的问题,我也想我知道如何解决它但不能。
完整的错误是
XMLHttpRequest cannot load http://1-1-51.wordbuzzweb.appspot.com/Login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://wordbuzzhtml5.appspot.com' is therefore not allowed access.
如您所见,这表明 请求的资源上不存在 'Access-Control-Allow-Origin' header。。但是,如果我查看所请求资源的 headers,它们如下所示。
Date: Thu, 18 Jun 2015 21:59:34 GMT
Content-Encoding: gzip
Server: Google Frontend
Vary: Accept-Encoding
Access-Control-Allow-Methods: GET, POST
Content-Type: application/json
Access-Control-Allow-Origin: *
Alternate-Protocol: 80:quic,p=0
Cache-Control: private
Access-Control-Allow-Headers: Content-Type
Content-Length: 127
如您所见,其中包含 Access-Control-Allow-Origin header。
如果有人能告诉我我做错了什么,将不胜感激。
使用POST方法请求header如下
Host: 192.168.254.1:8081
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8080/
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36
Accept: */*
Referer: http://localhost:8080/html/
Accept-Language: en-GB,en;q=0.8
Content-Length: 25
Content-Type: application/json
由于您在响应中得到一些 headers 返回,这很好地表明请求正在到达服务器,但是,它没有到达您的服务器路由这一事实表明问题是发出的请求与您的任何路线都不匹配。该请求可能是 OPTIONS 请求而不是 POST 请求,当您从不是 "simple request".
的浏览器发出 CORS 请求时,通常会发生这种情况
解决方案是将其设为 "simple request",或者让您的服务器响应 OPTIONS 请求。让您的服务器响应 OPTIONS 请求要容易得多,因为有时 "simple requests" 仍然会发送 OPTIONS 请求。
关于这个问题有很多问题,但我似乎仍然无法解决我的问题。
我正在尝试使用 HTML 5 Chrome 中的一款游戏。 Link here.
该游戏是使用 libgdx 编写的,我从我的应用引擎托管后端发布 json 数据。我读了很多书,我想我理解跨域访问的问题,我也想我知道如何解决它但不能。
完整的错误是
XMLHttpRequest cannot load http://1-1-51.wordbuzzweb.appspot.com/Login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://wordbuzzhtml5.appspot.com' is therefore not allowed access.
如您所见,这表明 请求的资源上不存在 'Access-Control-Allow-Origin' header。。但是,如果我查看所请求资源的 headers,它们如下所示。
Date: Thu, 18 Jun 2015 21:59:34 GMT
Content-Encoding: gzip
Server: Google Frontend
Vary: Accept-Encoding
Access-Control-Allow-Methods: GET, POST
Content-Type: application/json
Access-Control-Allow-Origin: *
Alternate-Protocol: 80:quic,p=0
Cache-Control: private
Access-Control-Allow-Headers: Content-Type
Content-Length: 127
如您所见,其中包含 Access-Control-Allow-Origin header。
如果有人能告诉我我做错了什么,将不胜感激。
使用POST方法请求header如下
Host: 192.168.254.1:8081
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:8080/
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.52 Safari/537.36
Accept: */*
Referer: http://localhost:8080/html/
Accept-Language: en-GB,en;q=0.8
Content-Length: 25
Content-Type: application/json
由于您在响应中得到一些 headers 返回,这很好地表明请求正在到达服务器,但是,它没有到达您的服务器路由这一事实表明问题是发出的请求与您的任何路线都不匹配。该请求可能是 OPTIONS 请求而不是 POST 请求,当您从不是 "simple request".
的浏览器发出 CORS 请求时,通常会发生这种情况解决方案是将其设为 "simple request",或者让您的服务器响应 OPTIONS 请求。让您的服务器响应 OPTIONS 请求要容易得多,因为有时 "simple requests" 仍然会发送 OPTIONS 请求。