Fetch API yields "TypeError: failed to fetch"

Fetch API yields "TypeError: failed to fetch"

我在 Chrome,我看到这个奇怪的错误:

TypeError: failed to fetch

(是的,这就是整个错误信息)。

这是产生错误的代码:

fetch(logoUrl, {
    method: 'put',
    headers: {
      'Content-Type': 'image/jpeg',
      //encode credentials as base64
      'Authorization': 'Basic ' + btoa('cdt-deployer:xyz'),
    },
    body: imgFile // the file

  }).catch(function(err){
       // the error appears here
  })

我该怎么办?我不知道出了什么问题。我只是想将二进制图像文件发送到另一台服务器(我看不到服务器日志)。

来自Fetch API documentation

A fetch() promise rejects with a TypeError when a network error is encountered, although this usually means a permissions issue or similar.

其中一个可能是 "network error" 的可能原因:

  • DNS 问题
  • 位于 logoUrl 的服务器不可用/有错误
  • 错误的 HTTP Headers 导致服务器忽略请求
  • 浏览器中止 -- 很可能是由于在获取完成之前导航到另一个页面