如何在NODEJS中使用Angular Ajax调用从Request POST对象获取对象?

时间:2015-07-30 05:22:13

标签: javascript ajax angularjs node.js mongodb

  1. 点击后按钮会调用方法..代码是,

        var express = require('express');
    
        var router = express();
    
        router.post('/adduser', function (req, res) {
    
        var db = req.db;
    
        var d1 = req.body;
    
        var collection = db.get('userlist');
        collection.insert(req.body, function (err, result) {
            res.send(
                (err === null) ? { msg: '' } : { msg: err }
            );
        });
        });
    
  2. 使用mongodb代码的节点服务是,

    <div class="container">
      <h1>Responsive Image Grid</h1>
    
        <hr>
    
      <div class="row">
        <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
         <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
         <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
         <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
         <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
         <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
         <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
         <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
        <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
        <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
        <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
        <div class="col-lg-2 col-sm-3 col-xs-4">
            <a href="#">
                 <img src="http://placehold.it/200x200" class="thumbnail img-responsive">
            </a>
        </div>
      </div>
      <hr>
      <br><br>
    
    </div>
    

    Http请求成功地用于调用服务方法。但我无法在服务方法中获取请求对象。我能为此做些什么?我想在服务方法中获取请求对象数据。请任何人帮助我。

1 个答案:

答案 0 :(得分:0)

body parser添加到您的快递应用。

var router = express();
bodyParser = require('body-parser');
router.use(bodyParser.json());