开发者问题收集

Angular 订阅 HTTPClient Observable,错误 req.url.toLowerCase 不是一个函数

2018-08-03
5988

我正在使用一项服务从 URL 中检索数据,我在另一个组件中使用该服务在前端显示数据...

我的服务:

getHttpData(baseurl, prefix?, suffix?, startTime?, endTime?, demo_id?)  {
    return this.http.get(baseurl+ prefix + suffix+ startTime + endTime + demo_id).map(
      (res: Response) => {return this.data = res;}
    )
  }

使用该服务的组件:

ngOnInit() {
baseurl: 'http://localhost:6666/some/tra/la/la98321'; 

    this.data = this.genHttp.getHttpData(this.baseurl).subscribe(result => {
      this.data = result;
      console.log('Daten:',this.data);
    });

  }

但是我在控制台中收到以下错误:

core.js:1633 ERROR TypeError: req.url.toLowerCase is not a function at HttpXsrfInterceptor.push../node_modules/@angular/common/fesm5/http.js.HttpXsrfInterceptor.intercept (http.js:2027) at HttpInterceptorHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptorHandler.handle (http.js:1407) at HttpInterceptingHandler.push../node_modules/@angular/common/fesm5/http.js.HttpInterceptingHandler.handle (http.js:2080) at MergeMapSubscriber.project (http.js:1158) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:117) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:107) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:93) at Observable._subscribe (scalar.js:5) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable._trySubscribe (Observable.js:176) at Observable.push../node_modules/rxjs/_esm5/internal/Observable.js.Observable.subscribe (Observable.js:161)

我该如何做才能检索数据而不出现错误?

2个回答

请尝试一下并告诉我

getHttpData(baseurl, prefix?, suffix?, startTime?, endTime?, demo_id?)  {
        const url= baseurl+ prefix + suffix+ startTime + endTime + demo_id+'';
        return this.http.get(url).map(
          (res: Response) => {return this.data = res;}
        )
      }
Rahul Cv
2018-08-03

对于我们来说,我们尝试使用非字符串值来调用 httpClient.post。

return this.httpClient.post(URL, formData, { withCredentials: true }).pipe(
    switchMap((presignedUrl: string) => this.httpClient.put(presignedUrl, file, { reportProgress: true, observe: 'events' }))
);

其中 presignedUrl 是一个对象 { url: '...' 。这很难从堆栈跟踪中诊断出来。

bobbyg603
2024-05-23