From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../access-control-allow-credentials/index.html | 94 ++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 files/zh-cn/web/http/headers/access-control-allow-credentials/index.html (limited to 'files/zh-cn/web/http/headers/access-control-allow-credentials') diff --git a/files/zh-cn/web/http/headers/access-control-allow-credentials/index.html b/files/zh-cn/web/http/headers/access-control-allow-credentials/index.html new file mode 100644 index 0000000000..7085ccb0f0 --- /dev/null +++ b/files/zh-cn/web/http/headers/access-control-allow-credentials/index.html @@ -0,0 +1,94 @@ +--- +title: Access-Control-Allow-Credentials +slug: Web/HTTP/Headers/Access-Control-Allow-Credentials +tags: + - CORS + - Reference + - header + - 响应头 + - 跨域 +translation_of: Web/HTTP/Headers/Access-Control-Allow-Credentials +--- +
{{HTTPSidebar}}
+ +

Access-Control-Allow-Credentials 响应头表示是否可以将对请求的响应暴露给页面。返回true则可以,其他值均不可以。

+ +

Credentials可以是 cookies, authorization headers 或 TLS client certificates。

+ +

当作为对预检请求的响应的一部分时,这能表示是否真正的请求可以使用credentials。注意简单的{{HTTPMethod("GET")}} 请求没有预检,所以若一个对资源的请求带了credentials,如果这个响应头没有随资源返回,响应就会被浏览器忽视,不会返回到web内容。

+ +

Access-Control-Allow-Credentials 头 工作中与{{domxref("XMLHttpRequest.withCredentials")}} 或Fetch API中的{{domxref("Request.Request()", "Request()")}} 构造器中的credentials 选项结合使用。Credentials必须在前后端都被配置(即the Access-Control-Allow-Credentials header 和 XHR 或Fetch request中都要配置)才能使带credentials的CORS请求成功。

+ + + + + + + + + + + + +
头部类型{{Glossary("Response header")}}
{{Glossary("Forbidden header name")}}no
+ +

语法

+ +
Access-Control-Allow-Credentials: true
+
+ +

指令

+ +
+
true
+
这个头的唯一有效值(区分大小写)。如果不需要credentials,相比将其设为false,请直接忽视这个头。
+
+ +

例子

+ +

允许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'
+})
+ +

规范

+ + + + + + + + + + + + + + +
规范状态注释
{{SpecName('Fetch','#http-access-control-allow-credentials', 'Access-Control-Allow-Credentials')}}{{Spec2("Fetch")}}Initial definition
+ +

浏览器兼容性

+ + + +

{{Compat("http/headers/access-control-allow-credentials")}}

+ +

查阅更多

+ + -- cgit v1.2.3-54-g00ecf