CORS GET在Firefox中返回一个空的响应主体

时间:2013-11-15 07:46:25

标签: javascript firefox backbone.js cors

从RESTful Backbone应用程序,我正在执行从mydomain.commyExtdomain.com的CORS请求。

在我的myExtdomain.com服务器上设置了CORS,我正在回复OPTIONS动词(任何网址):

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请求就会有效

无聊的细节a.k.a“The Headers”

回复OPTIONS动词:

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_

magic 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.

结束思路

  • 正如您所见,魔术GET请求的响应标头甚至不包括我在myExtdomain.com上设置的CORS标头
  • 另一方面,PUT请求确实包含了它们。
  • 同样,在Chrome中一切正常,所有响应标头都存在,我按预期得到JSON等等。
  • 我花了相当长的时间studying CORS(显然还不够),试图分解需要/不需要的东西而不是复制/粘贴随机代码
  • {li> JSONP GET次请求替代我
  • 我的所有请求(任何动词)均来自非安全页面( 来自https://
  • 我很绝望..

2 个答案:

答案 0 :(得分:15)

在我的所有API调用中添加Cache-Control: no-cache标头响应(在myExtdomain.com上)解决了我的问题:

Access-Control-Allow-Origin: *
Content-Type: application/json
Cache-Control: no-cache

出于某种原因,Firefox正在缓存我的API调用,并且在缓存时,FF无法再次解析JSON。在空响应体中结束或者出现任何错误..

现在我记得这不是我第一次强迫no-cache使用Firefox ..

同样,Chrome的一切正常,Chrome不需要Cache-Control: no-cache标题。

如果有人知道FF和Chrome之间存在这种差异(默认设置??),我会对此不再感兴趣。

希望这会节省一些时间。

答案 1 :(得分:0)

对我来说,解决方案是在服务器端的响应头中添加.parent { &.mother { // target elements classed `parent` AND `mother` .grandparent & { // target elements classed `parent` AND `mother` with a // `grandparent` ancestor. } } } :这是客户端Access-Control-Allow-Credentials: true对象设置request.withCredentials = true;的对称。