我的 app.component.ts 包含
login() {
var user = `{ email: ${this.email}, password: ${this.password} }`
const headers = new HttpHeaders()
.set('Authorization', 'my-auth-token')
.set('Content-Type', 'application/json');
this.http.post('http://localhost:3000/signin', user, {
headers: headers
}).subscribe(data => {
});
console.log(`email: ${this.email} password: ${this.password}`)
}
当尝试获取 Node 中的数据时,我得到
error: SyntaxError: Unexpected token e in JSON at position 2
我正在使用
req.body
获取数据。 解析 JSON 数据的正确方法是什么?还想知道这是否是将表单数据从 Angular 传递到 Node 的正确方法?
最佳答案
var user = {
email: this.email,
password: this.password
}
...
this.http.post('http://localhost:3000/signin',user).subscribe(...)
关于node.js - 如何将数据从 Angular 传递到 Node js服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406223/