从RESTful Backbone应用程序,我正在执行从mydomain.com
到myExtdomain.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
PUT
请求有效,但我的GET
请求“有点失败”...... 200 OK
JSON
。GET
请求就会有效回复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.
答案 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;
的对称。