From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../getallresponseheaders/index.html | 83 ++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 files/zh-cn/web/api/xmlhttprequest/getallresponseheaders/index.html (limited to 'files/zh-cn/web/api/xmlhttprequest/getallresponseheaders/index.html') diff --git a/files/zh-cn/web/api/xmlhttprequest/getallresponseheaders/index.html b/files/zh-cn/web/api/xmlhttprequest/getallresponseheaders/index.html new file mode 100644 index 0000000000..5c12e95526 --- /dev/null +++ b/files/zh-cn/web/api/xmlhttprequest/getallresponseheaders/index.html @@ -0,0 +1,83 @@ +--- +title: XMLHttpRequest.getAllResponseHeaders() +slug: Web/API/XMLHttpRequest/getAllResponseHeaders +translation_of: Web/API/XMLHttpRequest/getAllResponseHeaders +--- +

{{draft}}{{APIRef('XMLHttpRequest')}}

+ +

XMLHttpRequest.getAllResponseHeaders() 方法返回所有的响应头,以 {{Glossary('CRLF')}} 分割的字符串,或者 null 如果没有收到任何响应。 注意: 对于复合请求 ( multipart requests ),这个方法返回当前请求的头部,而不是最初的请求的头部。

+ +
DOMString getAllResponseHeaders();
+ +

语法

+ +
var headers = XMLHttpRequest.getAllResponseHeaders();
+
+ +

参数

+ +

无。

+ +

返回值

+ +

一个原始的header头例子:

+ +
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
+
+ +

每一行通过\r\n来进行分割。

+ +

例子

+ +
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;
+    });
+  }
+
+
+ +

上面的代码执行后,你可以:

+ +
var contentType = headerMap["content-type"];
+
+ +

上面的变量contentType可以获取到http header里的content-type字段值。

+ +

 

+ +

 

+ +

 

+ +

 

+ +

 

-- cgit v1.2.3-54-g00ecf