无法通过JavaScript fetch()发送自定义标头请求

时间:2019-06-24 11:55:24

标签: javascript json api header fetch

let header = new Headers();

header.append('Accept', 'application/json');

const key = {
    key: 'somejibberishcontent'
};
header.append('X-User-Key', key.key);

let req = new Request('https://jsonplaceholder.typicode.com/users', {
    method: 'GET',
    headers: header,
    mode: 'cors'
});

fetch(req)
    .then(response => {
        if (response.ok) {
            return response.json();
        } else {
            throw new Error();
        }
    })
    .then(data => console.log(data))
    .catch((err) => {
        console.log('ERROR: ', err.message);
    })

这就是我尝试通过互联网和YouTube查找的内容。但是我在devtool中看不到我请求的标头。

这是我请求的标头的样子。

**Requested Header**

GET /js/test.js HTTP/1.1
Host: 127.0.0.1:5500
Connection: keep-alive
DNT: 1
If-None-Match: W/"52a-16b894aa0c4"
If-Modified-Since: Mon, 24 Jun 2019 11:41:37 GMT
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36
Accept: */*
Referer: http://127.0.0.1:5500/test.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8,es;q=0.7

在请求的标头中找不到附加的标头。

0 个答案:

没有答案