formdata 没有到达后端

formdata is not reaching the backend

我正在我的应用程序中提交一个 html 表单,在从 angular 前端发送 http 请求之前,我在控制台中获取提交的数据,但数据没有到达 NodeJS 中的后端。当我从邮递员提交表单时,代码正在运行。

 <form [formGroup] = "myform" (ngSubmit) = "myFormSubmit()">
 <input type="text" placeholder="Name" formControlName="name">
 <div *ngFor="let data of dataArr;index as i>
 <input type="checkbox" id="1" [value]="data.id" name="{{ data.id }}" 
 (change)="getSelectedData($event)" formArrayName="checkboxData">
 </div>
 <button type="submit" [disabled] = "!myform.valid">Save</button>
 </form>

 myFormSubmit() {
   console.log(this.myform.value); // i can see all submitted form values here
   this.service.postmyForm(this.myform.value).subscribe(response =>{
 });
 }

 private headerData = {
 headers: new HttpHeaders({
  'Content-Type': 'application/x-www-form-urlencoded',
  'Content-Length': '541385555',
  'Authorization': `Bearer ${this.token}`
})
};

public postmyForm(formData): Observable<any> {
  console.log('formData'+ JSON.stringify(formData)); //i can see submitted data in console
  return this.http.post(this.url+ '/adduser', { formData }, this.headerData);
}

Nodejs Backend:-  
 @Post('/adduser')
  adduser(@Request() req) {
    console.log(req.body.name);return;  //Am not getting the form value here
  }

试试

return this.http.post(this.url+ '/adduser', formData, this.headerData);

没有 { } formData

原因是你已经通过 { } 你发送的正文将是

 { 
   "formData": {
              name: "something",
              ....
            }
          }

当您尝试在此处直接使用名称访问它时 console.log(req.body.name) 您的版本应该是 console.log(req.body.formData.name)

但是如果您删除 { } 它可以通过 console.log(req.body.name) 访问,因为对象将以以下形式传递

 { 
    "name": "something",
    ...
  }