【发布时间】: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