Angular 2+ 读取 http 响应头 [英] Angular 2+ read http response headers

查看:34
本文介绍了Angular 2+ 读取 http 响应头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到答案但找不到,所以我的问题很简单,我有这个功能

I was trying to find answer but was unable to, so my question is simple, i have this function

  getProducts(projectId, pageSize, page) {
    return new Promise((resolve, reject) => {
      this.http.get('/api/projects/' + projectId + '/products/?pageSize=' + pageSize + '&page=' + page).subscribe(data => {
        resolve(data);
      }, error => {
        reject(error)
      });
    });
  }

我正在尝试读取分页自定义标头,在浏览器响应中我可以看到它,但我不确定如何使用我的函数访问它,在旧的 angular 上我使用的是拦截器,但在 angular 2+ 上我'我无法让它工作,谁能告诉我怎么做?

I'm trying to read pagination custom header, and in browser response i can see it, but i'm not sure how to access it with my function, on old angular i was using interceptors but on angular 2+ i'm unable to make it work, anyone can show me how to do it ?

推荐答案

所以在我调查了许多可能性之后,我找到了最好和最简单的解决方案,我希望这对某人有所帮助.我使用 HttpClient 和 angular 4+,只需在 http.get 方法中添加新参数 {observe: 'response'}

So after i investigated many possibilities i found best and most simple solution, i hope this helps someone. I use HttpClient and angular 4+, just simply add new parameter in http.get methd {observe: 'response'}

  getProjects(projectId, pageSize, page) {
    return new Promise((resolve, reject) => {
      this.http.get('/api/projects/' + projectId + '/products/?pageSize=' + pageSize + '&page=' + page,
        { observe: 'response' }).subscribe(data => {
          const pagination = JSON.parse(data.headers.get('pagination'));
          resolve({
            items: data.body,
            totalItems: pagination.TotalItems
          });
        }, error => {
          reject(error)
        });
    });
  } 

这篇关于Angular 2+ 读取 http 响应头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆