我在从响应中读取状态代码时遇到一些问题。
我正在服务中调用api,
return this.http.get<string>( this.remoteServer + '/google.com/open', httpOptions);
在我的 Controller 中,
open() {
this.openService.open(this.selected.qrCode)
.subscribe(
data => {
console.log(data);
this.toastService.showSuccess('Unlock Successfull', 'success');
}
);
}
现在我想读取http状态文本,
我从上面的调用中得到的示例 http 响应是。
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "https://google.com/open", ok: false, …}
如何读取 Controller 中的状态文本。
请帮助我
最佳答案
您可以指定 {observe: 'response' }
作为 get
请求的第二个参数,该请求为您提供完整的响应数据。
如果您想连同 headers
或 params
一起发送其他选项,只需以这种方式将所有它们汇总到一个对象中即可。
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json'}), params : myParams, observe: 'response'}
return this.http.get<string>(
this.remoteServer + '/google.com/open', httpOptions).subscribe((data) => {
console.log(data.status); // staus 200
console.log(data.statusText); // OK
});
关于angular - 如何从 Angular http 的响应中读取状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51049806/