blob: 5c12e955264285aba5a1f0a673e311c9c2b3c312 (
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
---
title: XMLHttpRequest.getAllResponseHeaders()
slug: Web/API/XMLHttpRequest/getAllResponseHeaders
translation_of: Web/API/XMLHttpRequest/getAllResponseHeaders
---
<p>{{draft}}{{APIRef('XMLHttpRequest')}}</p>
<p><strong>XMLHttpRequest.getAllResponseHeaders()</strong> 方法返回所有的响应头,以 {{Glossary('CRLF')}} 分割的字符串,或者 <code>null</code> 如果没有收到任何响应。<strong> 注意:</strong> 对于复合请求 ( multipart requests ),这个方法返回当前请求的头部,而不是最初的请求的头部。</p>
<pre class="syntaxbox">DOMString getAllResponseHeaders();</pre>
<h2 id="语法">语法</h2>
<pre>var headers = <var>XMLHttpRequest</var>.getAllResponseHeaders();
</pre>
<h3 id="参数">参数</h3>
<p>无。</p>
<h3 id="返回值">返回值</h3>
<p>一个原始的header头例子:</p>
<pre><code>date: Fri, 08 Dec 2017 21:04:30 GMT\r\n
content-encoding: gzip\r\n
x-content-type-options: nosniff\r\n
server: meinheld/0.6.1\r\n
x-frame-options: DENY\r\n
content-type: text/html; charset=utf-8\r\n
connection: keep-alive\r\n
strict-transport-security: max-age=63072000\r\n
vary: Cookie, Accept-Encoding\r\n
content-length: 6502\r\n
x-xss-protection: 1; mode=block\r\n</code>
</pre>
<p>每一行通过\r\n来进行分割。</p>
<h2 id="例子">例子</h2>
<pre><code>var request = new XMLHttpRequest();
request.open("GET", "foo.txt", true);
request.send();
request.onreadystatechange = function() {
if(this.readyState == this.HEADERS_RECEIVED) {
// Get the raw header string
var headers = request.getAllResponseHeaders();
// Convert the header string into an array
// of individual headers
var arr = headers.trim().split(/[\r\n]+/);
// Create a map of header names to values
var headerMap = {};
arr.forEach(function (line) {
var parts = line.split(': ');
var header = parts.shift();
var value = parts.join(': ');
headerMap[header] = value;
});
}
</code>
</pre>
<p>上面的代码执行后,你可以:</p>
<pre><code>var contentType = headerMap["content-type"];</code>
</pre>
<p>上面的变量contentType可以获取到http header里的content-type字段值。</p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
|