Javascript 获取不能与 http://key@www.example.com 一起使用

Javascript fetch not working with http://key@www.example.com

我正在尝试访问 web-Api(确切地说是 Prestashops Web API),并且使用这种格式的 url 获取似乎有问题:http://SOMEKEY12345@www.example.com。在 Chrome 和 Postman 中,一切都很好,但是使用 fetch-API 我总是得到一个 401 错误。

这是我的打字稿代码:

public fetchData() {
fetch("http://SOMEKEY1234@www.example.com").then((response : Response)=>{
  response.text().then((text : string) =>console.log(text));
})

Fetch API 不允许在请求中使用 http://SOMEKEY1234@www.example.com 形式的 URL。具体来说,它不允许您在主机名前使用“@”字符。

如果您使用这样的 URL,它被解释为提供 username/password 凭据,并且 the Fetch spec requires browsers to throw a TypeError(从而停止请求):

The Request(input, init) constructor must run these steps:

If parsedURL includes credentials, then throw a TypeError.

唯一的解决办法是不要以这种方式在请求 URL 中放入凭据(也就是说,在请求 [=26] 的主机名前不要有“@”字符=]).