問題描述
我有一些代碼用于進行 fetch 調用.這利用了現代 chrome/firefox 中內置的 window.fetch api.
I have some code where i make a fetch call. This takes advantage of window.fetch api built into modern chrome / firefox.
代碼有時會遇到 401:unauthorized 響應.這是正常的,我希望它被忽略,我可以通過代碼流來做到這一點.但是,當我嘗試運行 Chrome 時,它??會顯示難看的 console.error 消息.
The code sometimes hits a 401:unauthorized response. This is normal and I want it ignored, which I can do with the flow of the code. However, Chrome does show an unsightly console.error message when I try to run it.
如何以編程方式防止此控制臺錯誤顯示在所有機器的開發控制臺中(即,沒有 chrome 開發過濾器或 tampermonkey 類型插件).
How can I PROGRAMMATICALLY prevent this console error from showing in the dev console on all machines (i.e., no chrome dev filters or tampermonkey type plugins).
這里有一個示例:
fetch("http://httpstat.us/401", {requiredStatus: 'ok'})
.then(function() {
console.log("pass!");
}).catch(function() {
console.log("fail!");
});
推薦答案
很遺憾,這無法做到,因為控制臺中的這種類型的消息是由 chrome 自己打印的.壓制這種類型的消息已經爭論了多年,但共識似乎是這條消息是可取的 - 查看此討論.
Unfortunately, this cannot be done, as this type of message in the console is printed by chrome itself. Repressing this type of message has been debated for years, but the consensus seems to be that this message is desirable - see this discussion.
以防萬一您有興趣:根據 此評論,我們看到此消息的原因是因為對資源檢索請求的響應進行了評估,并且消息是在上下文級別調度的.
Just in case you're interested: As per this comment, the reason we're seeing this message is because the response to resource retrieval requests is evaluated, and messages are dispatched at the context level.
從本質上講,chrome 的編寫方式不允許我們更改此效果,因此我們會收到錯誤消息.
Essentially, the way chrome was written does not allow us to change this effect, and thus we have the error messages.
這篇關于在 chrome 開發工具中隱藏 401 console.error 在 fetch() 調用中得到 401的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!