在使用 Chrome 开发者工具观察 HTTP 请求时,可能会发现这种情况:
即开发者工具显示的是“临时报头”,出现这种问题的原因和解决办法尚不得而知
但如果想知道跨域请求默认会携带哪些 headers, 不妨在控制台粘贴执行如下代码查看:
fetch("https://httpbin.org/headers", {
headers: new Headers({'Cache-Control': 'no-cache'})
}).then(resp =>
resp.json().then(d => {
console.log("Request headers:", JSON.stringify(d.headers, null, 2));
})
);
Request headers: {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7,zh-TW;q=0.6",
"Cache-Control": "no-cache",
"Host": "httpbin.org",
"Origin": "https://www.google.com.hk",
"Pragma": "no-cache",
"Referer": "https://www.google.com.hk/",
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
}