Angular 2 RC 4 Http post 未触发
Angular 2 RC 4 Http post not firing
我正在尝试为我的应用发出 post 请求,但它不起作用,我尝试了多种方法。我在 chrome 的网络选项卡下没有看到请求被触发。不确定问题是什么,我们将不胜感激。
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
@Injectable()
export class UserService{
private _url="users";
constructor(private _http: Http){
}
checkIfUsernameExists(username){
return this._http.get(this._url+"/"+username)
.map(response => response.json())
}
createUser(user){
let headers = new Headers();
headers.append('Content-Type', 'application/json')
console.log(JSON.stringify(user));
return this._http.post(this._url, JSON.stringify(user),{headers: headers})
.map(response => response.json())
}
}
尝试这样调用它,这样您就可以跟踪错误
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);
}
首先你需要捕获错误并解决错误:
this._http.post(this._url, JSON.stringify(user),{headers: headers})
.subscribe(
(res) => {
// on success
},
(err) => {
// on error
console.error(err);
}
使用 promise 调用服务
this.UserService.createuser(user)
.subscribe(
data => { this.responseModel= data },
error => console.log('error'),
() => this.getComplete() //complete function
);
我正在尝试为我的应用发出 post 请求,但它不起作用,我尝试了多种方法。我在 chrome 的网络选项卡下没有看到请求被触发。不确定问题是什么,我们将不胜感激。
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
@Injectable()
export class UserService{
private _url="users";
constructor(private _http: Http){
}
checkIfUsernameExists(username){
return this._http.get(this._url+"/"+username)
.map(response => response.json())
}
createUser(user){
let headers = new Headers();
headers.append('Content-Type', 'application/json')
console.log(JSON.stringify(user));
return this._http.post(this._url, JSON.stringify(user),{headers: headers})
.map(response => response.json())
}
}
尝试这样调用它,这样您就可以跟踪错误
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);
}
首先你需要捕获错误并解决错误:
this._http.post(this._url, JSON.stringify(user),{headers: headers})
.subscribe(
(res) => {
// on success
},
(err) => {
// on error
console.error(err);
}
使用 promise 调用服务
this.UserService.createuser(user)
.subscribe(
data => { this.responseModel= data },
error => console.log('error'),
() => this.getComplete() //complete function
);