HttpErrorResponse - 无法从服务器获得 text/html 响应

HttpErrorResponse - Cannot get the text/html response from server

我在从我的服务器获取 HTTP 响应时遇到问题。 我想将使用 HttpClient 的身份验证登录复制到我的 login.service.ts 文件中。

我在最后 2 小时尝试找到解决方案,但没有任何效果...

这是我使用的代码:

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 .

Content-Type 不是 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 

欢迎任何帮助!

我努力学习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