gpt4 book ai didi

javascript - Angular6 HttpClient : Inject Headers on Get request is not working on Chrome and IE11

转载 作者:行者123 更新时间:2023-11-30 20:21:19 26 4
gpt4 key购买 nike

在我的 Angular 6 应用程序下,我正在尝试使用一些 GET 请求,同时注入(inject)一些 Headers :

我的服务是这样的:

@Injectable()
export class MyService {
constructor(public httpClient: HttpClient) {
}
getUserInfos(cuid): Observable<any> {
const headers = new HttpHeaders({'login': login});
return this.httpClient.get(environment.urls.UserHabilitation, {headers});
}

在我的组件中:我像下面这样订阅它:

this.myService.getUserInfos(login).subscribe(infos => {
console.log(infos );
error => {
console.log(error);
});

Firefox 下运行良好,但在 Chrome 和 IE11 下运行不佳 ;我得到了这样一个错误:

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 () 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._trySubscribe (Observable.js:42) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:28) at subscribeTo.js:21 at subscribeToResult (subscribeToResult.js:6)

建议??

最佳答案

看起来你的 '}' 放错了地方。订阅可以获得 2 个参数,执行和错误响应:

this.myService.getUserInfos(login).subscribe(
infos => {
console.log(infos);
},
error => {
console.log(error);
}
);

或更短:

this.myService.getUserInfos(login).subscribe(
infos => console.log(infos ),
error => console.log(error)
);

关于javascript - Angular6 HttpClient : Inject Headers on Get request is not working on Chrome and IE11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51420397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com