无法获得POST请求以在angular2中工作

时间:2017-09-27 19:59:18

标签: angular

角色新手,我无法想出这个。 我的GET功能完美无缺,我可以通过邮递员发送此请求(http://localhost:3000/api/posts/?content=testentry)并更新我的数据库。

下面的代码来自我的services.ts表,我确实收到了服务器收到的日志"无论我输入什么"'但就我而言。 非常感谢任何帮助,谢谢。

getAllPosts() {
  return this.http.get('http://localhost:3000/api/posts')
    .map(res => res.json());
}

createPost(post) {
  console.log('server received ' + post);  
  let payload = new URLSearchParams();
  payload.set('content',post);
  var headers = new Headers();
  headers.append('Content-type', 'application/x-www-form-urlencoded');

  return this.http.post('http://localhost:3000/api/posts',payload.toString(), { 
    headers: headers
  }).map(res => res.json());
}

1 个答案:

答案 0 :(得分:0)

好的,所以这都是由于预期的愚蠢错误。在我的服务器端代码中,我有:Post.addPost(req.query.content,function(err,post)而不是Post.addPost(req.body.content,function(err,post)。 感谢大家帮忙,从你的日子里抽出时间。

相关问题