我的 Chrome 扩展执行了一个 get 请求,效果很好。由于使用代码片段测试速度更快,因此我想在 Chrome 控制台或 Chrome 代码片段中执行完全相同的操作。最小的例子:
fetch(url, {
method: "GET"
}).then(response => response.text())
.then(html => console.log(html))
.catch(error => console.log(error))
不幸的是,我只能得到
TypeError: Failed to fetch
对于错误和
Failed to load resource: net::ERR_FAILED
在 Chrome 的内联错误标记中
在我的 Chrome 扩展中,我遇到了 CORS 问题,因此我在 AWS Lambda 函数中所做的是将标头设置为
const headers = {
'Content-Type': 'application/json',
"Access-Control-Allow-Headers" : "Content-Type",
"Access-Control-Allow-Origin" : "*",
"Access-Control-Allow-Credentials" : true
};
so I supposeCORS 不是这里的问题。但我似乎无法弄清楚在扩展程序中运行请求与在控制台/代码片段中运行请求会产生什么差异。我在这里可能会缺少什么?
我也没有在 AWS CloudWatch 中看到该请求,所以我想它甚至没有离开我的机器。我正在对安装了 0 个扩展的 Chrome 用户进行测试,在隐身模式下也会发生同样的情况
为了解决我的服务器的任何问题,我还插入了来自的示例https://lockevn.medium.com/snippet-to-fetch-remote-rest-api-and-display-in-console-of-chrome-devtool-6896a7cd6041
async function testGet() {
const response = await fetch('https://jsonplaceholder.typicode.com/posts')
console.log(await response.json())
}
async function testPost() {
let r = await fetch('https://jsonplaceholder.typicode.com/posts', {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
lockevn: 1
}),
})
console.log(await r.json())
}
testGet()
testPost()
Chrome 的“网络”选项卡显示请求已停止
![enter image description here](https://i.stack.imgur.com/iQtmB.png)
链接的“解释”给出
Queueing:浏览器在以下情况下对请求进行排队:
Stalled:请求可能会因中所述的任何原因而被搁置Queueing.
更高的优先级似乎很奇怪,6 个连接也不是问题,因为我在测试之前重新启动了浏览器,而且磁盘缓存问题听起来也不像问题。我不是没有防病毒软件的 macOS