Access-Control-Allow-Credentials 响应头表示是否可以将对请求的响应暴露给页面。返回true则可以,其他值均不可以。
Credentials可以是 cookies, authorization headers 或 TLS client certificates.
当作为对预检请求的响应的一部分时,这能表示是否真正的请求可以使用credentials。注意简单的GET 请求没有预检,所以若一个对资源的请求带了credentials,如果这个返回这个资源,响应就会被浏览器忽视,不会返回到web内容。
Access-Control-Allow-Credentials 头 工作中与{domxref("XMLHttpRequest.withCredentials")}} 或Fetch API中的Request() 构造器中的credentials 选项结合使用。Credentials必须在前后端都被配置(即the Access-Control-Allow-Credentials header 和 XHR 或Fetch request中都要配置)才能使带credentials的CORS请求成功。
| Header type | Response header |
|---|---|
| Forbidden header name | no |
Syntax
Access-Control-Allow-Credentials: true
Directives
- true
- 这个头的唯一有效值(区分大小写)。如果不需要credentials,相比将其设为false,请直接忽视这个头。
Examples
允许credentials:
Access-Control-Allow-Credentials: true
使用带credentials的 XHR :
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/', true);
xhr.withCredentials = true;
xhr.send(null);
使用带credentials的 Fetch :
fetch(url, {
credentials: 'include'
})
Specifications
| Specification | Status | Comment |
|---|---|---|
| Fetch Access-Control-Allow-Credentials |
Living Standard | Initial definition |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
| Feature | Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | Servo |
|---|---|---|---|---|---|---|---|
| Access-Control-Allow-Credentials | 4 | 12 | 3.5 | 10 | 12 | 4 | ? |
| Feature | Android | Chrome for Android | Edge Mobile | Firefox for Android | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|---|
| Access-Control-Allow-Credentials | 2.1 | (Yes) | (Yes) | 1.0 | (Yes) | 12 | 3.2 |