responseType: "json" header causing error: Property 'responseStatus' does not exist on type 'Object'
responseType: "json" header causing error: Property 'responseStatus' does not exist on type 'Object'
我之前读过如果你将 responseType: "json"
header 添加到你的 http 请求中会更安全,所以我想添加它。
然而,当我使用它时,将响应视为 JSON(即使用点符号)时出现错误。
例如,当我尝试获取 json 响应的 属性 时,如下所示:res.responseStatus
,它会抛出错误:
"Property 'responseStatus' does not exist on type 'Object'."
如果我从 http 请求中取出 responseType: "json"
header,我不会收到错误。
如果我留在 responseType
header 中,那么我可以通过以下符号在响应中获得 object 的属性:res["responseStatus"]
而这不会抛出一个错误。
我只是想知道是否有人知道为什么。也许我错过了什么。
谢谢
Httppost请求码(来自ionicapp-angular5)
login(loginData): Observable<any>{
return this.http.post("http://localhost/api/auth",
{"emailAddress": loginData.emailAddress, "password": loginData.password},
{"headers": { "Content-Type": "application/json;charset=utf-8"}, responseType: "json"})
.map(res => {
if(res.responseStatus === "success"){
//Throwing error: "Property 'responseStatus' does not exist on type 'Object'."
//but If I say res["responseStatus"] instead this does not throw error.
//Or If I take out the responseType header, I can use the dot notation.
}
return res;
});
}
我假设您正在使用 HttpClient
。
您需要添加 observe: 'response'
to the httpOptions
以获得完整的回复。
我还将 res
的类型指定为 HttpResponse
。正如乔恩在他的评论中提到的那样,如果您在 httpOptions
.
中使用了 observe: 'response'
,那么这并不是真正需要的
此外,您将获得 status
而不是 responseStatus
。
试一试:
login(loginData): Observable < any > {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json;charset=utf-8'
}),
observe: 'response',
responseType: "json"
};
return this.http.post("http://localhost/api/auth", loginData, httpOptions)
.map((res: HttpResponse) => {
if (res.status === 200) {
...
}
return res;
});
}
我之前读过如果你将 responseType: "json"
header 添加到你的 http 请求中会更安全,所以我想添加它。
然而,当我使用它时,将响应视为 JSON(即使用点符号)时出现错误。
例如,当我尝试获取 json 响应的 属性 时,如下所示:res.responseStatus
,它会抛出错误:
"Property 'responseStatus' does not exist on type 'Object'."
如果我从 http 请求中取出 responseType: "json"
header,我不会收到错误。
如果我留在 responseType
header 中,那么我可以通过以下符号在响应中获得 object 的属性:res["responseStatus"]
而这不会抛出一个错误。
我只是想知道是否有人知道为什么。也许我错过了什么。
谢谢
Httppost请求码(来自ionicapp-angular5)
login(loginData): Observable<any>{
return this.http.post("http://localhost/api/auth",
{"emailAddress": loginData.emailAddress, "password": loginData.password},
{"headers": { "Content-Type": "application/json;charset=utf-8"}, responseType: "json"})
.map(res => {
if(res.responseStatus === "success"){
//Throwing error: "Property 'responseStatus' does not exist on type 'Object'."
//but If I say res["responseStatus"] instead this does not throw error.
//Or If I take out the responseType header, I can use the dot notation.
}
return res;
});
}
我假设您正在使用 HttpClient
。
您需要添加 observe: 'response'
to the httpOptions
以获得完整的回复。
我还将 res
的类型指定为 HttpResponse
。正如乔恩在他的评论中提到的那样,如果您在 httpOptions
.
observe: 'response'
,那么这并不是真正需要的
此外,您将获得 status
而不是 responseStatus
。
试一试:
login(loginData): Observable < any > {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json;charset=utf-8'
}),
observe: 'response',
responseType: "json"
};
return this.http.post("http://localhost/api/auth", loginData, httpOptions)
.map((res: HttpResponse) => {
if (res.status === 200) {
...
}
return res;
});
}