【发布时间】:2021-01-28 10:38:49
【问题描述】:
我想根据 HTTP 响应的状态代码显示一个带有文本的 toast。 服务代码:-
private options = {
headers: new HttpHeaders()
.set("Content-Type", "application/json")
.set("x-app-id", this.appId)
.set("x-app-key", this.appKey)
.set("observe","response")
};
constructor(private http: HttpClient) {}
search(query: string): Observable<{}> {
return this.http.post(this.url, {query: query}, this.options)
}
这就是我在前端所做的:-
search() {
this.showcard = false;
this.isLoading = true;
this.nutrix.search(this.searchValue).subscribe((res) => {
if(res){
this.isLoading = false;
this.showcard = true;
this.queryResult = res["foods"][0];
this.imageUrl = this.queryResult['photo']['thumb']
}
console.log(this.queryResult);
},(error) => {
console.log(error);
this.isLoading = false;
});
}
【问题讨论】:
-
你在错误回调中得到
console.log(error);,如果你没有状态,你也可以添加它.. -
在那里找到了那个状态码。谢谢!
标签: angular http error-handling http-response-codes