2017-08-25 22 views
0

我正在嘗試使用AngularJS向我的服務器發送一個相當簡單的POST請求。該請求通過,命中我在後端的控制器,但由於某種原因req.data被顯示爲undefined在AngularJS的Node.js中未定義數據POST請求

前端控制器:

function CardDirectiveController($http, $scope) { 
    var self = this; 

    self.addToFavorites = function() { 
     let card = { 
      id: $scope.$id, 
      attack : $scope.attack, 
      cost: $scope.cost, 
      img: $scope.img, 
      name: $scope.name, 
      class: $scope.class, 
      rarity: $scope.rarity, 
      type: $scope.type 
     } 

     return $http({ 
      method: 'POST', 
      url: '/card', 
      data: card 
     }) 
    }; 
} 
angular.module('cardDirective').controller('CardDirectiveController', CardDirectiveController); 

服務器

'use strict'; 

let express = require('express'), 
    path = require('path'), 
    router = require('./routes/sampleRouter'), 
    cardRouter = require('./routes/cardRouter'); 


let app = express(); 

// Serve any requests for static files (like index.html) 
app.use(express.static(path.join(__dirname + '/public/'))); 

// Use any routing rules found in the router file 
app.use('/', router); 
app.use('/card', cardRouter) 



app.listen(PORT, function() { 
    console.log('Application live and running on port: ' + PORT); 
}); 

路由器:

'use strict'; 

let express = require('express'), 
    cardController = require('./../controllers/cardController'); 

let router = express.Router(); 

router.route('/').post(cardController.postCard); 
router.route('/:cardName').get(cardController.showCards); 


module.exports = router; 

後端控制器

'use strict'; 

let cardController = { 
    showCards: showCards, 
    postCard: postCard 
}; 

module.exports = cardController 


function showCards(req, res) { 
    console.log('showCards ', req.params.cardName); 
    res.end() 
} 

function postCard(req, res) { 
    console.log('postCard ', req.url, req.method, req.data) 
    res.end() 
} 

我在控制檯作出這一請求得到的響應是postCard/POST undefined。控制檯記錄card對象返回預期的結果。我覺得我必須缺少明顯的東西,但我一直堅持了一段時間了。

+0

請用身體解析器中間件在app.js .. '''的https:// www.npmjs.com /包/身體parser'' ' –

回答

2

您需要使用bodyParser中間件解析請求正文。

安裝body-parser模塊:

$ npm install body-parser 

配置它app.js:

var bodyParser = require('body-parser'); 

// parse application/json 
app.use(bodyParser.json()); 

在你的控制器使用req.body代替req.data

function postCard(req, res) { 
    console.log('postCard ', req.url, req.method, req.body); 
    res.end(); 
} 
0

請使用體佩解析器在你的app.js文件中。 您server.js文件更改爲下面的代碼

'use strict'; 

    let express = require('express'), 
     path = require('path'), 
     router = require('./routes/sampleRouter'), 
     cardRouter = require('./routes/cardRouter'), 
     bodyParser = require('body-parser'); 




    let app = express(); 

    // Serve any requests for static files (like index.html) 
    app.use(express.static(path.join(__dirname + '/public/'))); 
    // parse application/json 
    app.use(bodyParser.json()); 

    // Use any routing rules found in the router file 
    app.use('/', router); 
    app.use('/card', cardRouter) 



    app.listen(PORT, function() { 
     console.log('Application live and running on port: ' + PORT); 
    });