【问题标题】:jQuery cookie CORS doesnt work even with headers set and withCredentials即使设置了标头和 withCredentials,jQuery cookie CORS 也不起作用
【发布时间】:2016-10-22 15:47:38
【问题描述】:

应用程序位于“http://192.168.1.5:8000”上,API 服务器位于“http://192.168.1.5:9000”上。我正在尝试使用 jQuery 将带有 cookie 的 GET 请求发送到应用程序中的 API 服务器。

这是完整的请求标头(从 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 因跨源策略而停止。

如何解决这个问题?需要更多标头吗?

【问题讨论】:

    标签: javascript jquery http cookies


    【解决方案1】:

    注意,您上面的响应标头使用通配符 * 作为允许来源标头,

    • 访问控制允许来源:*

    然而,CORS 规范表明,对于支持凭证的请求,不能将通配符用于该标头(使用显式原始值作为接受原始值)。 CF:https://www.w3.org/TR/cors/#resource-requests

    • Access-Control-Allow-Origin:“实际显式来源”

    我希望这些信息有所帮助, ..迈克·布拉蒂

    【讨论】:

      猜你喜欢
      • 2019-05-07
      • 2021-11-25
      • 2017-09-29
      • 1970-01-01
      • 2013-09-18
      • 2015-11-19
      • 2017-12-08
      • 2016-10-14
      • 1970-01-01
      相关资源
      最近更新 更多