![](/img/trans.png)
[英]App deployed on Heroku but api calls are now failing (Failed to load resource: net::ERR_CONNECTION_REFUSED, TypeError: Failed to fetch)
[英]JavaScript fetch ERR_CONNECTION_REFUSED vs TypeError Failed to fetch
當使用 JavaScript fetch API 時,瀏覽器的控制台會記錄錯誤ERR_CONNECTION_REFUSED
,但 fetch 的catch
塊返回一條消息TypeError Failed to fetch
當服務器離線時。
以下最小提取突出顯示了兩條錯誤消息:
fetch("https://localhost/get-something", {
method: "POST"
}).then(resp => {
console.log(resp)
}).catch(err => {
console.log("ERR:", err, err.name, err.message)
})
運行 fetch 時,瀏覽器的控制台會出現兩個錯誤:
POST https://localhost/get-something net::ERR_CONNECTION_REFUSED
和
ERR: TypeError: Failed to fetch
// err.message will be "failed to fetch"
在服務器關閉以外的實例中返回泛型failed to fetch
消息。 我對ERR_CONNECTION_REFUSED
的瀏覽器控制台錯誤更感興趣。
有沒有辦法在失敗的 fetch 調用中捕獲ERR_CONNECTION_REFUSED
值?
不幸的是,這是不可能的,因為這種消息是從 Chrome 本身打印在控制台中的。 多年來一直在爭論抑制這些類型的消息,但共識似乎是這些類型的消息是可取的。
您只能使用過濾器將它們從您的客戶端隱藏檢查: https://umaar.com/dev-tips/79-hide-network-console/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.