2012-07-26 84 views
1

在我的应用程序中,我需要使用body parser来请求参数(基于angular-express-blog的Node.js AngularJS)。例如(AngularJS控制器):在没有bodyParser的情况下从AngularJS的Node.js请求数据

$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中这个数据表示。我只能使用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.cofiguration:

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

+0

您使用快递,对不对?我可以看到你的app.configure()块吗?它应该可以工作,req.body是$ http发送的数据。 – 2012-07-27 17:24:57

+1

我用这个信息更新了帖子,发现'$ scope.comment'应该是'comment',现在就可以使用。谢谢你!:) – zishe 2012-07-27 18:05:24

+0

我以前试过,但我想我没有重新启动服务器或水手。 – zishe 2012-07-27 18:08:57

回答

1

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

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