javascript - Angular 无法获取响应状态文本

标签 javascript ajax angular httpclient

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 响应状态编号或文本

最佳答案

试试这个,你可以传入一个带有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)
});

关于javascript - Angular 无法获取响应状态文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49896600/

相关文章:

javascript - 如何使用 jquery 显示当前日期以及时间 AM PM 和 CST

javascript - 关于如何从表单发送值的简单 jQuery/Ajax 问题

php - 自动从 MySQL 检索数据

javascript - 使用 jQuery 获取选定行的隐藏列的值

javascript - 照片编辑器SDK : export to server without auto download?

angular - 动态(点击)功能 - Angular2

angular - 在 Ng-Select 中更改颜色

IE 中的 Angular App 空白页面但在 Chrome 中有效

javascript - 数据库值无法将其获取到 Dropdownlist jQuery

javascript - ReactJS:未捕获类型错误:无法读取未定义的属性 'setState'