最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fetch API yields "TypeError: failed to fetch" - Stack Overflow

programmeradmin3浏览0评论

I am on Chrome, and I see this bizarre error:

TypeError: failed to fetch

(yes, that is the whole error message).

Here is the code that generated the error:

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
  })

what am I supposed to do? I have no idea what's wrong. I am simply trying to send a binary image file to another server (I cannot see the server logs).

I am on Chrome, and I see this bizarre error:

TypeError: failed to fetch

(yes, that is the whole error message).

Here is the code that generated the error:

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
  })

what am I supposed to do? I have no idea what's wrong. I am simply trying to send a binary image file to another server (I cannot see the server logs).

Share Improve this question edited Mar 17, 2022 at 8:51 VLAZ 29.1k9 gold badges62 silver badges84 bronze badges asked Jul 28, 2017 at 23:53 Alexander MillsAlexander Mills 100k165 gold badges532 silver badges909 bronze badges 14
  • Please post full error. – emil Commented Jul 28, 2017 at 23:55
  • If you try in a different browser do you get the same error? Have you successfully done the PUT using some other client like curl or Postman or something? – sideshowbarker Commented Jul 29, 2017 at 0:09
  • 3 @emil that is the full error dude. I stated that in the question. – Alexander Mills Commented Jul 29, 2017 at 0:30
  • 1 To be clear: when you try it using curl or whatever, you’re also sending the image as the entity body, with a 'content-type: image/jpeg' header? (And as far as the possibility that the problem might be the image isn’t a jpeg image but instead a gif or png, even if that were the case I am pretty sure it wouldn’t be causing that TypeError to be thrown) – sideshowbarker Commented Jul 29, 2017 at 1:16
  • 1 Also wonder if it might be choking on imgFile. You might consider updating your question to show how imgFile is getting set – sideshowbarker Commented Jul 29, 2017 at 2:44
 |  Show 9 more ments

1 Answer 1

Reset to default 13

From the 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.

One of these may be possible causes of "network error":

  • DNS problems
  • the server at logoUrl is unavailable / erroneous
  • Erroneous HTTP Headers causing server to ignore the request
  • Browser aborted -- most likely due to navigation to another page before the fetch pletes
发布评论

评论列表(0)

  1. 暂无评论