angular - Angular 6 - 429 响应处理
问题描述
如何在 Angular 6 的 HttpClient 上捕获 429 错误响应?
下面的示例将正确捕获 401 错误,但对于 429,它会反馈一个未知错误。
错误/编辑:预检选项检查失败。
登录代码:
this.http.post<any>(this._loginUrl, this.loginUserData)
.subscribe(
res => {
console.log(res)
},
err => {
console.log(err);
if (err.status == 401) {
// unauthorized
} else if (err.status == 429) {
// limit reached
} else {
// unknown
}
}
)
401响应
HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", url: "http://localhost:82/api/login", ok: false, …}
429回应
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
解决方案
由于您的失败发生在预检 OPTIONS 请求上,这很可能意味着响应缺少 CORS 标头(这就是为什么您的状态为 0,而不是 429)。
推荐阅读
- autohotkey - WinWaitActive 不激活窗口
- python - jupyter错误:无法在随机森林中将决策树视为png
- ruby - 如何使用 RSA-SHA1 为 oAuth 1.0 签名基本字符串?
- swift - SwiftUI 中的 MVVM 和相应的绑定
- php - 为 Laravel 排队命令设置自定义超时
- javascript - 为什么最后一个`input.value`如下
- javascript - 如何在 React JS 中动态生成站点地图
- xaml - 如何在 Xamarin Form 中设置控件以在每个手机屏幕尺寸中获得相似和相关的 UI?
- java - 使用 powerMockito.whenNew() 和使用 doNoting(object) 模拟对象但本地创建的对象正在调用实际方法
- angular - 尝试使用 Jasmine 对数据服务执行端到端测试时遇到空注入器错误