aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/xmlhttprequest/withcredentials/index.html
blob: db46b8c6ed0967cbfb26c61970eb6fbba8cbd9b4 (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
---
title: XMLHttpRequest.withCredentials
slug: Web/API/XMLHttpRequest/withCredentials
tags:
  - AJAX
  - XMLHttpRequest
translation_of: Web/API/XMLHttpRequest/withCredentials
---
<p>{{APIRef('XMLHttpRequest')}}</p>

<p><strong>XMLHttpRequest.withCredentials  </strong>属性是一个{{jsxref("Boolean")}}类型,它指示了是否该使用类似cookies,authorization headers(头部授权)或者TLS客户端证书这一类资格证书来创建一个跨站点访问控制(cross-site <code>Access-Control</code>)请求。在同一个站点下使用<code>withCredentials属性是无效的。</code></p>

<p><code>此外,这个指示</code>也会被用做<code>响应中</code>cookies 被忽视的标示。默认值是false。</p>

<p>如果在发送来自其他域的XMLHttpRequest请求之前,未设置<code>withCredentials</code> 为true,那么就不能为它自己的域设置cookie值。而通过设置<code>withCredentials</code> 为true获得的第三方cookies,将会依旧享受同源策略,因此不能被通过<a href="/en-US/docs/Web/API/Document/cookie">document.cookie</a>或者从头部相应请求的脚本等访问。</p>

<div class="note">
<p><strong>注:</strong> 永远不会影响到同源请求</p>
</div>

<div class="note">
<p><strong>Note:</strong><strong> </strong>不同域下的<code>XmlHttpRequest</code> 响应,不论其<code>Access-Control-</code> header 设置什么值,都无法为它自身站点设置cookie值,除非它在请求之前将<code>withCredentials</code> 设为true。</p>
</div>

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

<pre class="brush: js">var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/', true);
xhr.withCredentials = true;
xhr.send(null);</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('XMLHttpRequest', '#the-withcredentials-attribute')}}</td>
   <td>{{Spec2('XMLHttpRequest')}}</td>
   <td>WHATWG living standard</td>
  </tr>
 </tbody>
</table>

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

{{Compat("api.XMLHttpRequest.withCredentials")}}