应用程序在“http://192.168.1.5:8000” ',并且 API 服务器位于 'http://192.168.1.5:9000 '.我正在尝试使用 jQuery 将带有 cookie 的 GET 请求发送到应用程序中的 API 服务器。
这是完整的请求 header (从 FireBug 中提取):
GET /api/rooms HTTP/1.1
Host: 192.168.1.5:9000
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://192.168.1.5:8000
Origin: http://192.168.1.5:8000
Cookie: COOKIE=bf27f9f2-6bf8-4688-ac03-fc802653ce22
Connection: keep-alive
响应头:
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Allow: GET, OPTIONS, POST
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: OPTIONS, GET, POST
Access-Control-Allow-Headers: Content-Type, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control
Date: Wed, 09 Dec 2015 15:33:50 GMT
Content-Length: 6670
jQuery 代码:
$.ajax('http://192.168.1.5:9000/api', {
xhrFields: {
withCredentials: true
},
dataType: 'json',
crossDomain: true,
type: 'GET',
success: function (response) {
console.log(response);
}
});
虽然浏览器收到响应(状态码为 200),但 jQuery 因跨源策略而停止。
如何解决这个问题?需要更多标题?
最佳答案
请注意,您上面的响应 header 使用通配符 * 作为允许来源 header ,
- 访问控制允许来源:*
然而 CORS 规范指出通配符不能用于该 header ,用于支持凭据的请求(改为使用显式原始值作为接受原始值)。 CF:https://www.w3.org/TR/cors/#resource-requests
- Access-Control-Allow-Origin:“实际的显式来源”
希望对你有所帮助 ..迈克布拉蒂
关于javascript - jQuery cookie CORS 即使设置了 header 和 withCredentials 也不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34183259/