作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一些代码,例如
fetch(url).then().catch
如果响应不是 200,如何使窗口显示警报?因为 then 和 catch 都在自己的上下文中,所以我无法执行通常的“window.alert”
最佳答案
Because with then and catch they are in their own context so I can't do the usual "window.alert"
当然可以。
alert on non-200 response from fetch?
你确定这就是你想要的吗?还有其他 2xx
状态代码也被认为很好。
尝试这样的事情......
fetch(url).then((res) => {
if (!res.ok) {
throw new Error('Response not OK', res);
}
return res;
}).then(/* your then handler */).catch((e) => {
alert('There was an error!!');
});
另请参阅:https://developer.mozilla.org/en-US/docs/Web/API/Response/ok
关于Javascript - 如何对 fetch 的非 200 响应发出警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54156054/
我是一名优秀的程序员,十分优秀!