【发布时间】:2016-04-22 22:36:51
【问题描述】:
我正在尝试让 Angular2 与我的 Asp.Net WebApi 2 服务器一起工作。我设法正确处理了一些 GET 请求,但是这个 POST 请求的行为很奇怪。我从服务器收到 OK (200) 响应,但以下代码将其视为错误:
public Register(){
this.accountService.Register(this.Name, this.Password, this.RepeatPassword, this.Email, this.Skype, this.Website).subscribe(
() => { //this is what's supposed to be called, but isn't
this.accountService.Login(this.Name, this.Password).subscribe(
res => {
console.log(res);
localStorage.setItem('token', res);
localStorage.setItem('user', this.Name);
this.router.navigate(['Home']);
},
error2 => {
console.log(error2.Message);
}
);
},
error => { //the response gets here, instead of being handled above
console.log(error.Message);
}
);
}
这里是accountService的Register方法:
public Register (userName:string, password:string, confirmPassword:string, email:string, skype:string, website:string)
{
return this.http.post(this.Uri + 'api/Account/Register', JSON.stringify(
{
UserName: userName,
Password: password,
ConfirmPassword: confirmPassword,
Email: email,
Skype: skype,
Website: website
}), this.GetRequestOptions() ).map((res: Response) => res.json());
}
【问题讨论】:
标签: angularjs ajax http asp.net-web-api2 response