为什么我在 Chrome 中看不到我的 HTTP headers?

Why can I not see my HTTP headers in Chrome?

我正在执行抓取:

fetch(url, fetchOptions);

fetchOptions 配置如下:

var fetchOptions = {
  method: options.method,
  headers: getHeaders(),
  mode: 'no-cors',
  cache: 'no-cache',
};

function getHeaders() {
  var headers = new Headers(); // Headers is part of the fetch API.
  headers.append('User-ID', 'foo');
  return headers;
}

在运行时检查 fetchOptions 如下所示:

fetchOptions.headers.keys().next() // Object {done: false, value: "user-id"}
fetchOptions.headers.values().next() // Object {done: false, value: "foo"}

但是 user-id 在每个 Chrome 开发工具的请求 header 中找不到:

GET /whatever?a=long_name&searchTerm=g HTTP/1.1
Host: host:8787
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36
Referer: http://localhost:23900/
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,fr;q=0.6

为什么我在 Chrome 开发工具中看不到我的 "User-ID" header,为什么 header 键显示为小写?

万一其他人有类似的问题,有两个可能的罪魁祸首:

  1. 我可能没有以正确的标志开始 Chrome。

当 运行 来自 Windows 快捷方式时,以下内容不起作用:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -enable-precise-memory-info -disable-web-security

当从 Windows 命令提示符 运行 时,以下内容确实有效:

C:\whatever.0.2564.82\application\chrome.exe  --disable-web-security --user-data-dir=C:\whatever\tmp\chrome 
  1. 向请求模式添加 no-cors 可能导致 OPTIONS 请求先于 GET 请求,并且服务器不支持 OPTIONS。