aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/request/credentials/index.html
blob: f7a57ad6c0f6f1d602c8a9ed4a32ad0fe119ee09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: Request.credentials
slug: Web/API/Request/credentials
tags:
  - API
  - Fetch
  - 属性
  - 证书
  - 请求
translation_of: Web/API/Request/credentials
---
<div>{{APIRef("Fetch")}}</div>

<p><strong><code>credentials</code></strong>{{domxref("Request")}}接口的只读属性,用于表示用户代理是否应该在跨域请求的情况下从其他域发送cookies。这与XHR的withCredentials 标志相似,不同的是有三个可选值(后者是两个):</p>

<ul>
 <li><code>omit</code>: 从不发送cookies.</li>
 <li><code>same-origin</code>: 只有当URL与响应脚本同源才发送 cookies、 HTTP Basic authentication 等验证信息.(浏览器默认值,在旧版本浏览器,例如safari 11依旧是omit,safari 12已更改)</li>
 <li><code>include</code>: 不论是不是跨域的请求,总是发送请求资源域在本地的 cookies、 HTTP Basic authentication 等验证信息.</li>
</ul>

<h2 id="语法">语法</h2>

<pre class="brush: js">var myCred = request.credentials;</pre>

<h3 id="Value">Value</h3>

<p>A {{domxref("RequestCredentials")}} value.</p>

<h2 id="举例">举例</h2>

<p>在以下代码中,我们使用{{domxref("Request.Request()")}}创建了一个新的request(为了一个与脚本在同一目录下的图片文件), 接着将request credentials存入一个变量:</p>

<pre class="brush: js">var myRequest = new Request('flowers.jpg');
var myCred = myRequest.credentials; // returns "same-origin" by default</pre>

<h2 id="说明">说明</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('Fetch','#dom-request-credentials','credentials')}}</td>
   <td>{{Spec2('Fetch')}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<p>{{Compat("api.Request.credentials")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
 <li><a href="/en-US/docs/Web/HTTP/Access_control_CORS">HTTP access control (CORS)</a></li>
 <li><a href="/en-US/docs/Web/HTTP">HTTP</a></li>
</ul>