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

时间:2016-07-29 11:22:09

标签: javascript express angular

我试图发布" 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 (明确)发布方法:

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

点击提交命令提示后,我得到&#39; undefined&#39; ,为什么?

1 个答案:

答案 0 :(得分:0)

尝试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);
      );
}