代码之家  ›  专栏  ›  技术社区  ›  Sangram Badi

如何使用角度获取响应头

  •  0
  • Sangram Badi  · 技术社区  · 7 年前

    组件:

    下面是我的组件,我在其中调用服务中存在的checkauthentication()函数。

    signin()。{
    this.loginservice.checkauthentication(this.login).subscribe(
    (RESP)=& gt;{
    console.log(resp.headers);
    让user=resp.headers.get('info');
    localstorage.setitem('user',json.stringify(user));
    
    let token=resp.headers.get('x-token');
    localstorage.setitem('x-token',标记);
    
    this.router.navigate(['/list']);
    },请
    
    (错误)=& gt;{
    this.login=new login();
    console.log(错误、错误、消息);
    swl(error.error.message,'重试','错误');
    }
    ;
    }
    < /代码> 
    
    

    服务:

    这是我的服务,我在这里调用RESTAPI。在REST API中,我正在对头进行配对

    构造函数(private http:httpclient)
    
    CheckAuthentication(登录):Observable<Any>。{
    让path=constant.base_URL+'api/login';
    常量标题={
    标题:新的httpheaders({
    'content type':'application/json'
    })
    };
    
    返回this.http.post(path,login,header);
    }
    < /代码> 
    
    

    下图是我在浏览器上看到的响应头。

    console.log(resp.headers);is showingundefined

    服务:

    这是我的服务,我在这里调用RESTAPI。在REST API中,我正在对头进行配对

    constructor(private http:HttpClient) { }
    
      checkAuthentication(login): Observable<any> {
          let path = Constant.BASE_URL + 'api/login';
          const header = {
            headers : new HttpHeaders({
              'Content-Type': 'application/json'
            })
          };
    
          return this.http.post(path, login, header);
      }
    

    下图是我在浏览器上看到的响应头。 enter image description here

    console.log(resp.headers);正在展示undefined

    2 回复  |  直到 7 年前
        1
  •  0
  •   Biplab Malakar    7 年前

    你需要过滤你的反应

    constructor(private http:HttpClient) { }
        checkAuthentication(login): Observable<any> {
              let path = Constant.BASE_URL + 'api/login';
              const header = {
                headers : new HttpHeaders({
                  'Content-Type': 'application/json'
                })
              };
    
              return this.http.post(path, login, header).map((res:Response)=> res);
          }
    

    现在,在订阅中,您还可以对头部和主体进行完全响应。现在你可以像

     signIn() {
        this.loginService.checkAuthentication(this.login).subscribe(
          (resp) => {
            console.log(resp.headers);
            let user = resp.headers.get('info');
            localStorage.setItem('user', JSON.stringify(user));
    
            let token = resp.headers.get('x-token');
            localStorage.setItem('x-token', token);
    
            this.router.navigate(['/list']);
          },
    
          (error) => {
            this.login = new Login();
            console.log(error.error.message);
            Swal(error.error.message, 'Try again.', 'error');
          }
        );
      }
    
        2
  •  0
  •   Sangram Badi    7 年前

    我把服务改成了下面的。结合@biplab malakar的答案和@trichtriche的评论。现在开始工作了

    checkAuthentication(login: Login): Observable<any> {
        let path = Constant.BASE_URL + 'api/login';
    
        return this.http.post(path, login, {
          headers: new HttpHeaders()
            .set('Content-Type', 'application/json'),
          observe: 'response'
        }).map((res) => res);
      }
    
    推荐文章