Angular 2 Http.get not responding

2020-04-15 05:42发布

I'm using Http.get to retrieve a configuration file. I've done this many times with success, but this time nothing. I'm not seeing the trace in my console, and I'm not seeing an error either, even though I am using .catch. What might be my problem here?

this._http.get('./assets/dashboard/json/config.json')
  .map((response: Response) => {

    console.log(response.json());

  })
  .catch((error: any) => Observable.throw(error || 'Server error'));

2条回答
别忘想泡老子
2楼-- · 2020-04-15 06:05

You need to have subscribe() in order to recieve the data

   this._http.get('./assets/dashboard/json/config.json')
        .map(res => res.json())
        .subscribe(
          data => this.ResResponse= data,
          err => this.logError(err),
          () => console.log('Completed')
        ); 

    logError(err) {
      console.error('There was an error: ' + err);
    }
查看更多
家丑人穷心不美
3楼-- · 2020-04-15 06:10

you need to call subscribe() in order that the request is executed.

查看更多
登录 后发表回答