我尝试访问返回明文响应的REST API。但是,当通过Angular5 HttpClient
发送请求时,我收到以下错误:
期间的Http失败
以下是我的请求的代码:
login(): void {
let resultPromise: Promise<string> = login();
resultPromise.then(token => this.onLogin(token), err => this.onLoginError(err));
}
login(loginData: LoginDataService): Promise<string> {
let registerDTO = loginData.toRegisterDTO();
const httpOptions = {
headers: new HttpHeaders({
responseType: 'text/plain'
})
};
return this.http.post<string>(this.userURL, registerDTO, httpOptions).toPromise();
}
private onLogin(token: string): void {
this.loginData.setToken(token);
this.router.navigate(["home"]);
}
private onLoginError(err: HttpErrorResponse) {
this.loginData.logout();
this.errorMsg = err.message;
}
但是,始终调用onLoginError
,传递以下对象:
{
"headers": {
"normalizedNames": {},
"lazyUpdate": null
},
"status": 200,
"statusText": "OK",
"url": "http://localhost:8080",
"ok": false,
"name": "HttpErrorResponse",
"message": "Http failure during parsing for http://localhost:8080",
"error": {
"error": {},
"text": "fd39196f-08e3-4c54-82dd-99756f544fe2"
}
}
我发现HttpResponse
不包含ContentType标头,即使Chrome显示响应中包含标题。