問題描述
我有一些代碼用于進(jìn)行 fetch 調(diào)用.這利用了現(xiàn)代 chrome/firefox 中內(nèi)置的 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.
代碼有時(shí)會(huì)遇到 401:unauthorized 響應(yīng).這是正常的,我希望它被忽略,我可以通過代碼流來做到這一點(diǎn).但是,當(dāng)我嘗試運(yùn)行 Chrome 時(shí),它??會(huì)顯示難看的 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.
如何以編程方式防止此控制臺(tái)錯(cuò)誤顯示在所有機(jī)器的開發(fā)控制臺(tái)中(即,沒有 chrome 開發(fā)過濾器或 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).
這里有一個(gè)示例:
fetch("http://httpstat.us/401", {requiredStatus: 'ok'})
.then(function() {
console.log("pass!");
}).catch(function() {
console.log("fail!");
});
推薦答案
很遺憾,這無法做到,因?yàn)榭刂婆_(tái)中的這種類型的消息是由 chrome 自己打印的.壓制這種類型的消息已經(jīng)爭(zhēng)論了多年,但共識(shí)似乎是這條消息是可取的 - 查看此討論.
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.
以防萬一您有興趣:根據(jù) 此評(píng)論,我們看到此消息的原因是因?yàn)閷?duì)資源檢索請(qǐng)求的響應(yīng)進(jìn)行了評(píng)估,并且消息是在上下文級(jí)別調(diào)度的.
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.
從本質(zhì)上講,chrome 的編寫方式不允許我們更改此效果,因此我們會(huì)收到錯(cuò)誤消息.
Essentially, the way chrome was written does not allow us to change this effect, and thus we have the error messages.
這篇關(guān)于在 chrome 開發(fā)工具中隱藏 401 console.error 在 fetch() 調(diào)用中得到 401的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!