簡體   English   中英

AngularJS POST請求在Node.js上未定義數據

[英]Data undefined at Node.js from AngularJS POST request

我正在嘗試使用AngularJS向我的服務器發送一個相當簡單的POST請求。 該請求通過並到達后端的我的控制器,但是由於某種原因req.data顯示為undefined

前端控制器:

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對象將返回預期結果。 我覺得我肯定想念一些明顯的東西,但是我已經被卡住了一段時間了。

您需要使用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();
}

請在app.js文件中使用body-parser。 將您的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);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM