【发布时间】:2021-09-04 06:54:47
【问题描述】:
假设,我有这个函数,提供了一个错误的 'uri' 字符串来获取:
function imgChanger(uri) {
fetch('uri', { mode: 'cors' })//WRONG 'uri' to demonstrate that catch doesn't prevent console's red error
.then(function (response) {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
}).then(function (response) {
img.src = response.data.images.original.url;
}).catch((err)=>{
console.log(err);
});
}
尽管处理了错误,但我可以在控制台中看到 404 消息:
对此我有两个问题:为什么在捕捉到它的情况下仍然显示它,我该如何处理?
【问题讨论】:
-
网址不存在
-
在我的浏览器控制台上运行相同的代码,我有黑色的
SyntaxError: Unexpected token < in JSON at position 0 -
标签: javascript google-chrome web console