在RESTful Backbone应用程序中,我正在处理从mydomain.com
到myExtdomain.com
的CORS请求。
我 * 确实 * 在我的myExtdomain.com
服务器上设置了CORS,我使用以下命令响应OPTIONS
动词(任何URL):
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
Status Code: HTTP/1.1 204 No Content
对于我在myExtdomain.com
上的API调用:
Access-Control-Allow-Origin: *
Content-Type: application/json
Status Code: HTTP/1.1 200 OK
我甚至不顾一切地尝试用所有东西来响应myExtdomain.com
上的所有HTTP请求:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
Content-Type: application/json
Status Code: HTTP/1.1 200 OK
问题是
- 在Chrome中一切正常
- 在Firefox中,我的
PUT
请求工作,但是我的GET
请求"有点失败"...
"有点失败"定义
- 返回的HTTP状态代码为
200 OK
- 但是响应是空的(没有响应正文/大小为0 KB)..它应该是一些
JSON
。 - 但是,出于某种原因,每100次中只有一次
GET
请求有效
无聊的细节又名"标题"
响应OPTIONS
predicate :
REQUEST HEADERS
-----------------
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0
Origin: http://mydomain.com
Host: www.myExtdomain.com
Connection: keep-alive
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: content-type
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
RESPONSE HEADERS
-----------------
X-Powered-By: ASP.NET
Server: Microsoft-IIS/7.0
Date: Fri, 15 Nov 2013 07:01:57 GMT
Content-Type: text/html
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE
Access-Control-Allow-Headers: Content-Type
一个PUT
请求:
REQUEST HEADERS
----------------
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0
Referer: http://mydomain.com/account
Origin: http://mydomain.com
Host: www.myExtdomain.com
Content-Type: application/json; charset=UTF-8
Content-Length: 36
Connection: keep-alive
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: application/json, text/javascript, */*; q=0.01
RESPONSE HEADERS
----------------
X-Powered-By: ASP.NET
Server: Microsoft-IIS/7.0
Date: Fri, 15 Nov 2013 07:01:57 GMT
Content-Type: application/json
Content-Length: 0
Access-Control-Allow-Origin: *
BODY RESPONSE
--------------
_Some_Json_Here_
- 神奇的 *
GET
请求:
REQUEST HEADERS
----------------
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:25.0) Gecko/20100101 Firefox/25.0
Referer: http://mydomain.com/somepage
Origin: http://mydomain.com
Host: www.myExtdomain.com
Connection: keep-alive
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Accept: application/json, text/javascript, */*; q=0.01
RESPONSE HEADERS
----------------
Server: Microsoft-IIS/7.0
Last-Modified: Fri, 15 Nov 2013 06:58:18 GMT
Date: Fri, 15 Nov 2013 07:01:57 GMT
Content-Type: application/json
Content-Length: 4041
Connection: keep-alive
RESPONSE BODY
--------------
Empty (0KB), it's supposed to be some JSON, that *SOMETIMES* (1/100) I get.. Magic.
结束的想法
2条答案
按热度按时间edqdpe6u1#
将
Cache-Control: no-cache
标头添加到我所有的API调用响应(在myExtdomain.com
上)解决了我的问题:由于某种原因,Firefox缓存了我的API调用,缓存后,FF无法再次解析JSON ..最终出现空响应体或任何错误..
现在我想起来了,这已经不是我第一次用Firefox强制
no-cache
了。再说一次,Chrome的一切都很好,Chrome不需要
Cache-Control: no-cache
标头。如果有人知道FF和Chrome之间的区别(默认设置??),我很想知道更多。
希望这能为某人保存一些时间。
7eumitmz2#
对我来说,解决方案是在服务器端的响应头中添加
Access-Control-Allow-Credentials: true
:这与在客户端侧为XMLHttpRequest
对象设置request.withCredentials = true;
是对称的。