Rxjs HttpErrorResponse-无法从服务器获取文本/html响应

Rxjs HttpErrorResponse-无法从服务器获取文本/html响应,rxjs,observable,angular7,angular-httpclient,Rxjs,Observable,Angular7,Angular Httpclient,我无法从服务器获取http响应。 我想使用HttpClient将身份验证登录复制到login.service.ts文件中 我在最后两个小时试图找到一个解决方案,但没有任何效果 以下是我使用的代码: login.service.ts import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Injectable({ providedIn: 'root' })

我无法从服务器获取http响应。 我想使用HttpClient将身份验证登录复制到login.service.ts文件中

我在最后两个小时试图找到一个解决方案,但没有任何效果

以下是我使用的代码:

login.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class LoginService {
  constructor(private http: HttpClient) { }

  login(){
    this.http.post('http://localhost:3000/auth', null, {headers: {
        'x-login': 'admin', 
        'x-password': '8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918'}})
  .subscribe(myToken => console.log(myToken));
  }
}
我使用PostMan测试登录名和密码,/auth响应令牌:653db89e-b80e-47f8-834a-62275b04fcbb

内容类型不是JSON响应,这是text/html;charset=utf-8,我想这可能就是问题所在,但我真的不知道

错误是:

HttpErrorResponse {headers: HttpHeaders, status: 201, statusText: "Created", url: "http://localhost:3000/auth", ok: false, …}
error: {error: SyntaxError: Unexpected number in JSON at position 1 at JSON.parse (<anonymous>) at XMLHttp…, text: "08951337-479a-4e53-8b1b-823d29d89139"}
headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
message: "Http failure during parsing for http://localhost:3000/auth"
name: "HttpErrorResponse"
ok: false
status: 201
statusText: "Created"
url: "http://localhost:3000/auth"
__proto__: HttpResponseBase 
HttpErrorResponse{headers:HttpHeaders,状态:201,状态文本:“已创建”,url:http://localhost:3000/auth“,ok:false,…}
错误:{error:SyntaxError:JSON中位于XMLHttp…,JSON.parse()位置1处的意外数字,文本:“08951337-479a-4e53-8b1b-823d29d89139”}
Header:HttpHeaders{normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ}
消息:“在解析的过程中Http失败。”http://localhost:3000/auth"
名称:“HttpErrorResponse”
好:错
现状:201
状态文本:“已创建”
url:“http://localhost:3000/auth"
__原型:HttpResponseBase
欢迎任何帮助

我试着学习Angular 7如果我说的不清楚, 请随时向我询问更多信息


谢谢

我将
reponseType:“text”
参数添加到我的post()中,它成功了! 这就是完整的操作:

this.http.post('http://localhost:3000/auth', null, {headers: {
        'x-login': 'admin', 
        'x-password': '8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918'},
responseType: "text" })
.subscribe(myToken => console.log(myToken)); // Output : 43307067-0c23-47ae-8085-402959e1f157

你试过
this.http.post吗http://localhost:3000/auth“,null,{headers:{…},responseType:{text})
?这可能会帮你解决问题:我使用了@Kos的建议,并添加了
reponseType:“text”
参数,多亏了你们两个,它成功了!