如何从 angular http 中的响应中读取状态?
How to read the status from response in angular http?
我在从响应中读取状态代码时遇到了一些问题。
我正在呼叫 api 服务,
return this.http.get<string>( this.remoteServer + '/google.com/open', httpOptions);
在我的控制器中,
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, …}
如何读取控制器中的状态文本。
请帮帮我
data.status
会给你想要的吗?
您可以将 { 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
});
可以将 {observe:'response'} 选项限定符添加到 HttpClient 对象的 get 或 put 中。它应该作为一个额外的逗号分隔字段添加到您可能已经定义的任何现有选项中。请参阅下面的示例,将简单的 Put 语句修改为 return 完整的 http 内容。这可用于您的应用程序中的标准化状态报告。
body = this.currentDocument;
let url = environment.base_url + 'api/Document/updateDocument';
// For a put the options block is the third parameter. For a get this would be the second
this.httpClient.put(url, body,
{
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
// Add the observe block to the existing option parameters
observe:'response'
})
.subscribe(
response => {
statusMessage = new StatusMessage();
statusMessage.HttpStatus = response.status;
statusMessage.StatusText = response.statusText;
// This example uses an EventEmitter but you could do any kind of logging here
this.updateStatusPanel.emit(statusMessage);
// The JSON body of this response can be accessed via the response.body parameter
},
err => {}
);
我们也可以通过在请求中添加 { observe: 'response' } 来获得完整的响应。
return this._http.get<any>(this.serverUrl+'categories/'+id, { observe: 'response' })
我在从响应中读取状态代码时遇到了一些问题。
我正在呼叫 api 服务,
return this.http.get<string>( this.remoteServer + '/google.com/open', httpOptions);
在我的控制器中,
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, …}
如何读取控制器中的状态文本。
请帮帮我
data.status
会给你想要的吗?
您可以将 { 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
});
可以将 {observe:'response'} 选项限定符添加到 HttpClient 对象的 get 或 put 中。它应该作为一个额外的逗号分隔字段添加到您可能已经定义的任何现有选项中。请参阅下面的示例,将简单的 Put 语句修改为 return 完整的 http 内容。这可用于您的应用程序中的标准化状态报告。
body = this.currentDocument;
let url = environment.base_url + 'api/Document/updateDocument';
// For a put the options block is the third parameter. For a get this would be the second
this.httpClient.put(url, body,
{
headers: new HttpHeaders({ 'Content-Type': 'application/json' }),
withCredentials: true,
// Add the observe block to the existing option parameters
observe:'response'
})
.subscribe(
response => {
statusMessage = new StatusMessage();
statusMessage.HttpStatus = response.status;
statusMessage.StatusText = response.statusText;
// This example uses an EventEmitter but you could do any kind of logging here
this.updateStatusPanel.emit(statusMessage);
// The JSON body of this response can be accessed via the response.body parameter
},
err => {}
);
我们也可以通过在请求中添加 { observe: 'response' } 来获得完整的响应。
return this._http.get<any>(this.serverUrl+'categories/'+id, { observe: 'response' })