create a synchronous http.get()

前端 未结 2 1367
长情又很酷
长情又很酷 2021-01-17 05:17

Im trying to handle a login via promises and http.get but i fail so hard I get following error :

Object doesn\'t support property or method \'toPromis

2条回答
  •  梦毁少年i
    2021-01-17 05:29

    I wonder if you actually use promise since the HTTP support of Angular relies on Observables.

    To get the response, you simply need to return the observable for your call:

    getSomething() {
      return this.http.get('http://localhost:5000/login/', {
        headers: authHeader
      }).map((response) => {
        return response.json()
      })
    }
    

    When calling the method, you can then register callbacks using the subscribe method:

    getSomething().subscribe(
      data => handleData(data),
      err => reject(err));
    

    If you really want to use promises (with the toPromise method), you should import this:

    import 'rxjs/Rx';
    

    See this issue for more details: https://github.com/angular/angular/issues/5632#issuecomment-167026172.

    Otherwise, FYI calls aren't synchronous regarding HTTP in browsers...

    Hope it helps you, Thierry

提交回复
热议问题