Angular 6 - 429 响应处理
Angular 6 - 429 response handling
如何在 Angular 6 中捕获 HttpClient 上的 429 错误响应?
下面的示例将正确捕获 401 错误,但对于 429,它会反馈未知错误。
Error/Edit: 预检选项检查失败。
登录码:
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 headers(这就是您获得状态 0 而不是 429 的原因)。
如何在 Angular 6 中捕获 HttpClient 上的 429 错误响应?
下面的示例将正确捕获 401 错误,但对于 429,它会反馈未知错误。
Error/Edit: 预检选项检查失败。
登录码:
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 headers(这就是您获得状态 0 而不是 429 的原因)。