【问题标题】:CORS preflight call doesn't follow with the GET callGET 调用不跟随 CORS 预检调用
【发布时间】:2014-02-18 17:33:34
【问题描述】:

我的 JavaScript 代码需要对 REST 服务进行以下 GET Ajax 调用:

jQuery.support.cors = true;
$.ajax({
    type: "GET",
    url: "https://serverdomain.com/serviceurl,
    cache: false,
    dataType: "json",
    timeout: 5000,
    crossDomain: true,
    headers: { 'Authorization': 'Bearer ' + access_token },
    success: function (data) {
        renderInfo(data);
    },
    error: function (jqxhr, textStatus, error) {
        ...
    }
});

由于 Authorization HTTP 标头,触发了以下 CORS 预检调用。

OPTIONS https://stcuatsoagw51.uatingdircan.ca:8443/sean/v1/customers/my/?_=1390845096145 HTTP/1.1
Accept: */*
Origin: https://localhost:44300
Access-Control-Request-Method: GET
Access-Control-Request-Headers: accept, authorization
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Host: stcuatsoagw51.uatingdircan.ca:8443
Content-Length: 0
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache

我更改了服务器端以向 OPTIONS 调用返回以下响应:

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: GET, PUT, POST, DELETE
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Content-Type: text/xml;charset=UTF-8
Content-Length: 27
Date: Mon, 27 Jan 2014 17:51:37 GMT

响应本质上是允许所有类型的 CORS 调用。但是,浏览器在收到 OPTIONS 调用响应后似乎停止了,并且没有继续发送原始 GET 调用。

我是否在响应 OPTIONS 调用时遗漏了任何内容?

【问题讨论】:

  • 您在开发人员/javascript 控制台中看到了什么?此外,如果 Access-Control-Allow-Credentials 为真,则不能对 Access-Control-Allow-Origin 使用通配符。您真的需要将 cookie 与您的跨域 GET 请求一起发送吗?
  • 谢谢雷。去掉两个通配符后,它起作用了。

标签: javascript ajax cors


【解决方案1】:

对于Access-Control-Allow-Headers,您不能使用通配符。您必须列出标题。此外,正如 Ray 所说,如果 Access-Control-Allow-Credentials 设置为 true,则不能为 Access-Control-Allow-Origin 使用通配符。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-09-13
  • 1970-01-01
  • 2022-06-29
  • 2014-03-24
  • 1970-01-01
  • 2014-02-06
  • 2016-06-11
  • 1970-01-01
相关资源
最近更新 更多