Getting “TypeError: failed to fetch” when the request hasn't actually failed

前端 未结 5 1723
予麋鹿
予麋鹿 2020-12-01 09:05

I\'m using fetch API within my React app. The application was deployed on a server and was working perfectly. I tested it multiple times. But, suddenly the application stopp

相关标签:
5条回答
  • 2020-12-01 09:38

    If your are invoking fetch on a localhost server, use non-SSL unless you have a valid certificate for localhost. fetch will fail on an invalid or self signed certificate especially on localhost.

    0 讨论(0)
  • 2020-12-01 09:40

    I have a similar problem and as I'm newbie, here are some facts for somebody to comment:

    I'm sending form data to Google sheet this way (scriptURL is https://script.google.com/macros/s/AKfy..., showSuccess() is showing a simple image):

    fetch(scriptURL, {method: 'POST', body: new FormData(form)})
    .then(response => showSuccess())
    .catch(error => alert('Error! ' + error.message))
    

    Executed in Edge my HTML doesn't show error and Network tab reports this 3 requests: Executed in Chrome my HTML (index.htm) shows Failed to fetch error and Network tab reports this 2 requests: The value in the second column is blocked:other and there is also an error in Console tab:

    GET https://script.googleusercontent.com/macros/echo?user_content_key=D-ABF... net::ERR_BLOCKED_BY_CLIENT

    In order to suspend installed Chrome extensions, I executed my code in an Incognito window and there is no error message and Network tab reports this 2 requests:

    My guess is that something (extension?) prevents Chrome to read the request's answer (the GET request is blocked).

    0 讨论(0)
  • 2020-12-01 09:49

    The issue could be with the response you are receiving from back-end. If it was working fine on the server then the problem could be with the response headers. Check the Access-Control-Allow-Origin (ACAO) in the response headers. Usually react's fetch API will throw fail to fetch even after receiving response when the response headers' ACAO and the origin of request won't match.

    0 讨论(0)
  • 2020-12-01 09:50

    Note that there is an unrelated issue in your code but that could bite you later: you should return res.json() or you will not catch any error occurring in JSON parsing or your own function processing data.

    Back to your error: You cannot have a TypeError: failed to fetch with a successful request. You probably have another request (check your "network" panel to see all of them) that breaks and causes this error to be logged. Also, maybe check "Preserve log" to be sure the panel is not cleared by any indelicate redirection. Sometimes I happen to have a persistent "console" panel, and a cleared "network" panel that leads me to have error in console which is actually unrelated to the visible requests. You should check that.

    Or you (but that would be vicious) actually have a hardcoded console.log('TypeError: failed to fetch') in your final .catch ;) and the error is in reality in your .then() but it's hard to believe.

    0 讨论(0)
  • 2020-12-01 09:55

    I know it's a relative old post but, I would like to share what worked for me: I've simply input "http://" before "localhost" in the url. Hope it helps somebody.

    0 讨论(0)
提交回复
热议问题