node.js - 如何将数据从 Angular 传递到 Node js服务器?

标签 node.js angular

我的 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/

相关文章:

内有较长任务的 Angular 间隔管道

Angular 将对象转换为数组

angular - 如何设置 Angular 5 中的 <ng-option> 值

node.js - Sequelize : effects of "unique" property in model definition

mysql - 当行值不等于特定值时更新表

javascript - Redis 异步查询过早完成的可能解决方案

javascript - Angular 4 和 Node.js

node.js - jhipster/spring boot - 尝试代理请求时发生错误

javascript - 退出 NodeJS 流

java - 带有 Spring Boot 和 Spring Security 的 Angular2