2016-07-15 92 views
0

我想为我的应用程序开启一个请求并且它不工作,我尝试了多种方法。我看不到在chrome中的网络选项卡下发出的请求。不知道问题是什么,任何帮助表示赞赏。Angular 2 RC 4 Http post not firing

​​

}

+0

从哪里你'createUser'获得呼叫服务? –

+0

我错过了在组件中添加订阅,之后我补充说它工作正常。惊讶没有错误。 – user2192298

+0

我认为地图已被删除,它只用于非常古老的阿尔法版本。 –

回答

1

首先,你需要捕获错误和解决错误:

this._http.post(this._url, JSON.stringify(user),{headers: headers}) 
     .subscribe(
     (res) => { 
      // on success 
     }, 
     (err) => { 
      // on error 
      console.error(err); 
     } 
0

尝试这样称呼它,所以你可以跟踪误差

let headers = new Headers({ 'Content-Type', 'application/json' }); 
let options = new RequestOptions({ headers: headers }); 

this.http.post(this._url, JSON.stringify(user),{headers: headers}) 
     .toPromise() 
     .then((res: Response) => { 
      let body = res.json(); 
      this.authService.fillAuthDataFromLogin(body); 
      this.router.navigate(['/Home']); 
      return body.data || {}; 
     }) 
     .catch(this.handleError); 

在这种方法处理错误

private handleError(error: any) { 
    ////debugger; 
    let errMsg = (error.message) ? error.message : 
     error.status ? `${error.status} - ${error.statusText}` : 'Server error'; 
    console.error(errMsg); // log to console instead 
    return Observable.throw(errMsg); 
    } 
0

呼叫使用承诺

 this.UserService.createuser(user) 
      .subscribe(
      data => { this.responseModel= data }, 
      error => console.log('error'), 
      () => this.getComplete() //complete function 
     );