Angular 2 http.post 发送空对象
Angular 2 http.post sending empty object
这是一个 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
袖子还有别的花招吗?
上面的代码有什么问题?
在这里发布答案,可能会对某人有所帮助。感谢@cartant 的指点。我只需要仔细看看:
- 我不需要明确设置 headers。默认情况下,post 发送
Content-Type: application/json
我没有必须对要发送的数据进行字符串化。
login(email: string, password: string): Observable<boolean> {
// this is optional
// let headers = new Headers({ 'Content-Type': 'application/json' });
// let options = new RequestOptions({ headers: headers });
return this.http.post(
apiURL + '/admin/login',
{ email: email, password: password },
// options
)
.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;
}
});
}
这是一个 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
袖子还有别的花招吗?
上面的代码有什么问题?
在这里发布答案,可能会对某人有所帮助。感谢@cartant 的指点。我只需要仔细看看:
- 我不需要明确设置 headers。默认情况下,post 发送
Content-Type: application/json
我没有必须对要发送的数据进行字符串化。
login(email: string, password: string): Observable<boolean> { // this is optional // let headers = new Headers({ 'Content-Type': 'application/json' }); // let options = new RequestOptions({ headers: headers }); return this.http.post( apiURL + '/admin/login', { email: email, password: password }, // options ) .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; } }); }