Angular $ http.get和Express req.body为空

时间:2016-06-07 06:18:23

标签: javascript angularjs node.js express

我在使用$ http.get发送数据的req.body时遇到问题。当我在routes.js方法中将路由从GET更改为POST,并将我的服务更改为$ http.post时,一切正常,但是使用GET,我无法将任何数据发送到节点中的服务器。任何人有任何想法?

server.js

// modules =================================================
var express        = require('express');
var app            = express();
var mongoose       = require('mongoose');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var path = require('path');

// configuration ===========================================


var db = require('./config/db');

var port = process.env.PORT || 8080; 
mongoose.connect(db.url); 
app.use(express.static(__dirname + '/public')); 


app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 



// routes ==================================================
require('./app/routes')(app,__dirname);

// start app ===============================================
app.listen(port);   
console.log('Magic happens on port ' + port);   
exports = module.exports = app; 

./应用程序/ routes.js

module.exports = function(app, __dirname) {

    var Card     = require('./models/card');

    app.post('/create', function(req, res) {
        var card = new Card();

        card.polishWord = req.body.polishWord;
        card.polishDescription = req.body.polishDescription;
        card.englishWord = req.body.englishWord;
        card.englishDescription = req.body.englishDescription;
        card.category = req.body.category;

        card.save(function(err){
            if(err){
                res.send(err);
            }
            res.json({message: 'Card created'});
        });
    });

    app.get('/take', function(req, res) {
        var condition = req.body.condition || {};
        console.log(req.headers);
        console.log('______________');
        console.log(req.body);
        console.log('______________');
        /*TODO :: Czemu nie odbiera parametrow GET*/
        Card.find(condition,function(err, cards) {
            if (err)
                res.send(err);

            res.json(cards);
        });
    });



    app.get('*', function(req, res) {
        res.sendFile('/public/index.html',{"root": __dirname});
    });
};

./ public / js / services / CardService(part)

cardService.getAllCards = function(){
    return $http.get('/take',{params: {condition:{"category":"animal"}},data: {condition:{"category":"animal"}}});
};

req.headers

{ host: 'localhost:8080',
  connection: 'keep-alive',
  accept: 'application/json, text/plain, */*',
  'user-agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML,
like Gecko) Chrome/50.0.2661.102 Safari/537.36',
  referer: 'http://localhost:8080/card',
  'accept-encoding': 'gzip, deflate, sdch',
  'accept-language': 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4',
  cookie: '_ga=GA1.1.1073910751.1465203314' }

.package.json(依赖项)

  "dependencies": {
    "express": "~4.13.1",
    "mongoose": "4.4.20",
    "body-parser": "~1.15.1",
    "method-override": "~2.0.2"
  },

有人有想法吗?

1 个答案:

答案 0 :(得分:1)

req.body未定义,因为您从'GET'发出了$http.get('/take')请求。您发送到服务器的是query parameters。查询参数的示例是:

http://stackoverflow.com/questions/tagged/npm?filter=all&sort=active

其中filtersort是分别为值'all''active'的查询参数。

要访问Express服务器中的查询参数,您需要使用req.query对象。它包含查询参数及其对应的值。 req.query请求的'/take'对象如下所示:

{
  condition: {
    category: "animal"
  }
}