Angular 2 TS-在服务器上未定义POST

Angular 2 TS - On POST getting undefined on server

本文关键字:未定义 POST 服务器 TS- Angular      更新时间:2023-09-26

我试图发布"uid",但服务器显示"undefined"。我不知道为什么会发生这种情况!!即使我正确设置了标题,它仍然不起作用,请帮帮我。

这里的应用程序代码:

HTML

<form (ngSubmit)="onSubmit()">
  User Id: <input type="text" name="uid" [(ngModel)] = 'uid'>  <br>
  <input type="submit" value="Submit">
</form>

onSubmit功能

onSubmit(value) {
  var headers = new Headers();
  var data = { uid: this.uid };
  headers.append('Content-Type', 'application/json');
  this.http.post('http://192.168.50.193:1000/process_post', JSON.stringify(data), {headers: headers})
      .subscribe((data)=>
          console.log("POST::: "+data);
      );
}

server.js(express)此处发布方法:

app.post('process_post', urlencodedParser, function (req, res) {
   response = {
       first_name:req.body.uid
   };
   console.log(req.body.uid);
   res.end(JSON.stringify(response));
})

单击Submit命令提示符后,我得到'undefined',为什么?

尝试map方法:

onSubmit(value) {
  var headers = new Headers();
  var data = { uid: this.uid };
  headers.append('Content-Type', 'application/json');
  this.http.post('http://192.168.50.193:1000/process_post', JSON.stringify(data), {headers: headers})
      .map(data => data.json())
      .subscribe((data)=>
          console.log("POST::: "+data);
      );
}