gpt4 book ai didi

javascript - 在 Chrome 扩展程序中创建自定义 header 失败

转载 作者:行者123 更新时间:2023-12-02 23:06:58 25 4
gpt4 key购买 nike

我尝试为以下 fetch() 创建一个 header ,如下所示

var myheaders = new Headers(
{ "Accept": "application/json",
"Content-Type": "application/json; charset=UTF-8"
});

let b = JSON.stringify ({ "cmd2" : "ytdl", "url" : "x"});

let params =
{ headers : myheaders,
body : b,
method : "POST",
mode : "no-cors"
};
let response = await fetch("http://127.0.0.1:5000/ytdl",params);
....

如果我在接收服务器(Flask)中打印标题,我会得到:

Host: 127.0.0.1:5000
Connection: keep-alive
Content-Length: 67
Accept: application/json
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Sec-Fetch-Mode: no-cors
Content-Type: text/plain;charset=UTF-8
Origin: chrome-extension://mnihgjnpmkpgeichhdfhejagbefjpnnb
Sec-Fetch-Site: cross-site
Accept-Encoding: gzip, deflate, br
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7

有什么想法吗,我做错了什么?

最佳答案

我不明白原因是什么,但是当您在没有 mode: 'no-cors' 的情况下调用时,内容类型是:

let params = {
headers : myheaders,
body : b,
method : "POST",
mode : "cors"
};

response = await fetch("http://127.0.0.1:5000/", params);

flask 的输出request.headers:

...
Accept: application/json
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36
Sec-Fetch-Mode: cors
Content-Type: application/json; charset=UTF-8
...

关于javascript - 在 Chrome 扩展程序中创建自定义 header 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545229/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com