【问题标题】:Angular can't get response status textAngular 无法获取响应状态文本
【发布时间】:2018-04-18 09:52:47
【问题描述】:
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);
    });
  }
}

所以这个 ajax 被发送到我的 spring 服务器 并且服务器将数据正确地保存在数据库中,基本上一切正常。

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

【问题讨论】:

    标签: javascript ajax angular httpclient


    【解决方案1】:

    试试这个,你可以传入一个带有观察键的对象以获得完整的响应

    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)
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-28
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多