【发布时间】:2017-01-09 12:26:29
【问题描述】:
这是一个 authservice 登录,取自 from here:
login(email: string, password: string): Observable<boolean> {
return this.http.post(
apiURL + '/admin/login',
JSON.stringify({ email: email, password: password }))
.map((response: Response) => {
// login successful if there's a jwt token in the response
let token = response.json() && response.json().token;
if (token) {
// set token property
this.token = token;
// store username and jwt token in local storage to keep user logged in between page refreshes
localStorage.setItem('currentUser', JSON.stringify({ email: email, token: token }));
// return true to indicate successful login
return true;
} else {
// return false to indicate failed login
return false;
}
});
}
在我的组件中,我尝试这样订阅:
login() {
this.loading = true;
console.log(this.model);
this.authenticationService.login(this.model.email, this.model.password)
.subscribe(result => {
if(result === true) {
this.router.navigate(['/']);
} else {
this.error = 'Username or password is incorrect';
this.loading = false;
}
});
}
我已经完成了控制台日志以确定数据是否实际被传递,是的。一切正常。
除了,发送的数据就是{ }
根据我的 Express 控制台,这是唯一通过 req.body 发送的内容。 req.headers表示内容类型正确,即Content-Type: application/json
我尝试使用 POSTMan 向 API 端点发送相同的 json 请求。工作正常。
Angular 2 的http.post 袖子还有什么技巧吗?
上面的代码有什么问题?
【问题讨论】:
-
好的,数据是否通过?这里有点矛盾。一方面你说数据被传递,另一方面你说数据只是:
{ }:) -
@AJT_82 很抱歉造成混乱。我的意思是,一个空对象
{ }是 http.post 发送的唯一内容。 -
什么是
this.model? -
我希望
JSON.stringify({ email: email, password: password },)对结尾的逗号不满意;您能否查看并确保这是minimal reproducible example? -
我更倾向于关注this example
标签: angular express angular2-http