在我的应用程序中,我需要使用主体解析器来请求参数(Node.js AngularJS 基于 角度表达博客)。例如(AngularJS 控制器):

$scope.changeComment = (comment) ->
  $http.put('/api/post/' + $routeParams.id + '/editComment/' + comment._id, $scope.comment).success (data) ->
    $scope.post = data.post

所以根据 AngularJS 文档 $http.post('/someUrl', data).success(successCallback);

但我不知道如何在node.jsexpress中找到这些数据。我只能使用 bodyParser,它只解析表单中的数据。

app.put '/api/post/:id/editComment/:cid' = (req, res) ->
  id = req.params.id;
  cid = req.params.cid;
  console.log req
  Post.findById id, (err, post) ->
    unless err
      comment = post.comments.id(cid)
      console.log req.body
      comment.text = req.body.text
      post.save (err1) ->

那么如何传输和抓取数据呢?

应用程序配置:

app.configure "development", ->
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use express.static(__dirname + '/public')
  app.use express.errorHandler(
    dumpExceptions: true
    showStack: true
  )

并查看文件 https://gist.github.com/3189377

有帮助吗?

解决方案

语法错误 $scope.comment 应该是公正的 comment:

$scope.changeComment = (comment) ->
  $http.put('/api/post/' + $routeParams.id + '/editComment/' + comment._id, comment).success (data) ->
    $scope.post = data.post
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top