Angular 6:HttpHeaders 无法在 Chrome 和 IE11(而不是 Firefox)中读取 null 的属性“长度" [英] Angular 6 : HttpHeaders Cannot read property 'length' of null in Chrome and IE11 (and not in Firefox)

查看:15
本文介绍了Angular 6:HttpHeaders 无法在 Chrome 和 IE11(而不是 Firefox)中读取 null 的属性“长度"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Angular6 应用程序下,我使用 HttpClient 和一些标头注入到我的 htpp 调用以从我的后端服务器获取数据:

Under my Angular6 app , i'm using HttpClient with some headers injection to my htpp calls to fetch data from my backend server :

我的服务:

@Injectable()
export class LoadUserInfosService {

  public _headers = new HttpHeaders().set('Content-Type', 'application/json');

  constructor(private httpClient: HttpClient) {}

  getUserPnsInfos(cuid): Observable<object> {
    const headers = this._headers.append('login', login);
    return this.httpClient.get(myBackUrl, {headers: headers});
  }
}

我订阅的组件:

loadUserInfosFromPns(login) {
    this.loadUserInfosService.getUserPnsInfos(login).subscribe(infos => {
      let receivedInfos: any;
        receivedPnsInfos = infos ;
        console.log(receivedPnsInfos);
        if (pnsInfos !== null && receivedPnsInfos.cuid === cuid ) {
        } else {
          this.router.navigate(['unauthorized'], {skipLocationChange: true});
        }
      },
      error => {
        console.log(error);
      });
  }

在 Chrome 或 IE11 上运行时,我收到一些关于我的请求标头的错误:

when running on Chrome or IE11 i'm getting some error refering to my request headers:

TypeError: Cannot read property 'length' of null
    at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.applyUpdate (http.js:199)
    at http.js:170
    at Array.forEach (<anonymous>)
    at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.init (http.js:170)
    at HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.forEach (http.js:235)
    at Observable._subscribe (http.js:1445)
    at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:161)
    at subscribeTo.js:21
    at subscribeToResult (subscribeToResult.js:6)
    at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._innerSub (mergeMap.js:127)

此错误仅出现在 ChromeIE11 中 - 而在 Firefox 下,请求会好起来,我不明白

this error appears only in Chrome and IE11 - while under Firefox the request get well and i'm not getting it

任何想法、建议?

推荐答案

这可能是因为您在某些 http 请求中设置了未定义/空标头.

It is probably caused because you are settting an undefined / null header in some http request.

这篇关于Angular 6:HttpHeaders 无法在 Chrome 和 IE11(而不是 Firefox)中读取 null 的属性“长度"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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