【问题标题】:Request data in Node.js from AngularJS without bodyParser从没有bodyParser的AngularJS请求Node.js中的数据
【发布时间】:2012-07-25 09:55:11
【问题描述】:

在我的应用程序中,我需要使用正文解析器来请求参数(Node.js AngularJS 基于angular-express-blog)。例如(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.js express 中找到这些数据。我只能使用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

【问题讨论】:

  • 你用的是快递,对吧?我可以看到你的 app.configure() 块吗?它应该可以正常工作,req.body 是 $http 发送的数据。
  • 我用这个信息更新了帖子,发现$scope.comment 应该是comment,它现在可以工作了。谢谢你!:)
  • 我之前试过但我想我没有重启服务器或smth。

标签: node.js request angularjs express


【解决方案1】:

语法错误$scope.comment 应该只是comment

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

【讨论】:

    猜你喜欢
    • 2014-12-08
    • 2018-05-07
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2013-10-14
    • 2019-11-26
    • 2011-02-07
    • 1970-01-01
    相关资源
    最近更新 更多