Angular无法获取响应状态文本

Ale*_*der 3 javascript ajax httpclient angular

export class GitComponent implements OnInit {
  http: HttpClient;
  headerForAjax: HttpHeaders;

  constructor(http: HttpClient) {
    this.http = http;
  }

  ngOnInit(): void {
    const headers = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzYXNobyIsImF1dGgiOiJST0xFX1VTRVIiLCJleHAiOjE1MjQwODcyMzJ9.MUv5RgI9LxQyrrCfjfX8HR2-XiQmz4vjLqH7V_0Du7VFLC0WrK_y3FfeNoT2Nj_uguIK2ss7jv-LNiHuCGtz4A';
    this.headerForAjax = new HttpHeaders().set('Authorization', headers);
    const linkBindingModel = {
      title: 'angular2',
      linkUrl: 'angularUr2l',
      imageUrl: 'imageUrl22'
    };
    this.http.post('http://localhost:8080/links/add', linkBindingModel, {headers: this.headerForAjax}).subscribe((e) => {
      console.log(e);
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

所以这个ajax被发送到我的spring服务器并且服务器正确地将数据保存在DB中,它基本上都运行良好.

但我无法获得响应状态,我的意思是如何获取我的服务器发回的200响应状态号或文本

Fat*_*med 5

试试这个,你可以使用observe键传入一个对象来获得完整的响应

this.http.post('http://localhost:8080/links/add', linkBindingModel, {headers: 
  this.headerForAjax, observe: 'response'}).subscribe((response) => {
  console.log(response.status); // response status 
  console.log(response.body); // response body (returned response)
});
Run Code Online (Sandbox Code Playgroud)