in Angular 4 HttpClient 如何接收状态码?
in Angular 4 HttpClient how can I receive status code?
获取数据我正在做:
data = this.http.get(url, httpOptions);
但这只是返回 body。我需要整个响应才能获得状态。我知道这个语法:
data = this.http.get(url, {observe: 'response'});
但这将取代我的 httpOpttions
,这将使我无法通过身份验证。我不能像在 POST
中那样在 GET
上添加另一个参数。请帮忙!
使用此代码获取状态。
更新于 [15/02/19]:
getOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json;charset=UTF-8',
"authToken": this.token // contains the authToken as parameter in request header
// of http.get method for authorisation.
})
};
// For getting the whole response of the request including status code etc.
getOptions['observe'] = 'response';
return this.http.get(url, getOptions)
.pipe(
catchError(this.handleError)
)
.subscribe(res => {
console.log(res);
},
err => {console.log(err)} );
以上更新的代码将导致给出整个响应
你不能给你的 http.get
添加第三个参数的原因是它不接受第三个参数。 observe
"syntax" 是 httpOptions
参数的一部分,因此您需要做的就是将 httpOptions
对象中的内容与 {observe: "response"}
[=34= 合并]
例如,如果您的 httpOptions
看起来像:
const httpOptions = {
headers: {
"Content-Type": "application/json"
}
}
您可以像这样将它与上面的 observe
对象结合起来:
const httpOptions = {
headers: {
"Content-Type": "application/json"
},
observe: "response"
}
如果你接受 httpOptions
作为参数(所以你不能像前面的例子那样从头开始创建一个新的),你可以直接在上面写 observe
字段:
httpOptions.observe = "response"
这两种方法中的任何一种都会保留您当前的 httpOptions
对象并向其添加 observe: "response"
字段。
编辑
要使此方法起作用,您需要 "lie" 向编译器提供有关 observe
的类型以允许其编译。您可以通过将 as any
添加到 httpOptions
对象中 "response"
的末尾来实现:
const httpOptions = {
headers: {
"Content-Type": "application/json"
},
observe: "response" as any
}
之所以需要这样做是因为 TypeScript 无法正确推断原始 httpOptions
对象的类型(它希望 "response"
是文字 "body"
)。告诉 TypeScript 将 "response"
解释为 any
可以解决这个问题。
获取数据我正在做:
data = this.http.get(url, httpOptions);
但这只是返回 body。我需要整个响应才能获得状态。我知道这个语法:
data = this.http.get(url, {observe: 'response'});
但这将取代我的 httpOpttions
,这将使我无法通过身份验证。我不能像在 POST
中那样在 GET
上添加另一个参数。请帮忙!
使用此代码获取状态。
更新于 [15/02/19]:
getOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json;charset=UTF-8',
"authToken": this.token // contains the authToken as parameter in request header
// of http.get method for authorisation.
})
};
// For getting the whole response of the request including status code etc.
getOptions['observe'] = 'response';
return this.http.get(url, getOptions)
.pipe(
catchError(this.handleError)
)
.subscribe(res => {
console.log(res);
},
err => {console.log(err)} );
以上更新的代码将导致给出整个响应
你不能给你的 http.get
添加第三个参数的原因是它不接受第三个参数。 observe
"syntax" 是 httpOptions
参数的一部分,因此您需要做的就是将 httpOptions
对象中的内容与 {observe: "response"}
[=34= 合并]
例如,如果您的 httpOptions
看起来像:
const httpOptions = {
headers: {
"Content-Type": "application/json"
}
}
您可以像这样将它与上面的 observe
对象结合起来:
const httpOptions = {
headers: {
"Content-Type": "application/json"
},
observe: "response"
}
如果你接受 httpOptions
作为参数(所以你不能像前面的例子那样从头开始创建一个新的),你可以直接在上面写 observe
字段:
httpOptions.observe = "response"
这两种方法中的任何一种都会保留您当前的 httpOptions
对象并向其添加 observe: "response"
字段。
编辑
要使此方法起作用,您需要 "lie" 向编译器提供有关 observe
的类型以允许其编译。您可以通过将 as any
添加到 httpOptions
对象中 "response"
的末尾来实现:
const httpOptions = {
headers: {
"Content-Type": "application/json"
},
observe: "response" as any
}
之所以需要这样做是因为 TypeScript 无法正确推断原始 httpOptions
对象的类型(它希望 "response"
是文字 "body"
)。告诉 TypeScript 将 "response"
解释为 any
可以解决这个问题。