简体   繁体   中英

Why is my request body empty?

I recently started learning the MEAN stack and I have encountered a problem. I am trying to send an $http.get request from angular to my local server but the request body is undefined. What's peculiar is that it is not undefined in my post. I am aware that the issue might be the body-parser and I have spent a couple hours trying to figure it out to no avail. Thanks for your help!

Here is my express code:

var express = require('express');
var mongoose= require('mongoose');
var bodyParser=require('body-parser');
var http=require('http');
var db=mongoose.connect('mongodb://localhost:27017/checkIt');



var server = express();
var Schema= mongoose.Schema;



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

server.use(bodyParser.json());


server.use(express.static(__dirname));



var UserSchema = new Schema({
    username: String,
    password: String
});

var User=mongoose.model('User',UserSchema);


server.post("/checkIt/users",function(req,res){

    var newUser = new User({
        username: req.body.username,
        password: req.body.password
    });

    newUser.save(function(err,doc){
        res.send("inserted");
    });

});

server.get("/checkIt/users",function(req,res){
    console.log(req.body);
    var userToCheck= new User({
        username:req.body.username,
        password:req.body.password
    });

    User.findOne({username: userToCheck.username, password: userToCheck.password},function(err,obj){
        res.send(obj);
    });

});
server.listen(3000);

Here is my loginController where I have my get request:

angular.module('app')
.controller('loginController',['$scope','$location', '$http',function($scope,$location,$http){


    $scope.checkIfUser = function(){
        var user={
            username:$scope.username,
            password:$scope.password
        };

        $http.get("http://localhost:3000/checkIt/users",user)
            .success(function(response){
                if(response===""){
                    console.log("User does not exist");
                }
                else goToHome();
            });
    };

    var goToHome = function(){
        $location.path('/Home')
    }

}]);

Lastly, I do not know if this will help or not but this is the code snippet of where I do my $http.post request

$scope.signup = function(){
            createUser();
            console.log(user);
            $http.post("http://localhost:3000/checkIt/users",user)
            .success(function(response){
                console.log(response);
            });
        };

There isn't going to be a body for a GET ! This is just how life is. No body for GET . Now to solve the problem. You want to use req.query on the server side to access the values.

On the Angular side you need to make a slight change to your code (sending passwords in urls is a bad thing):

$http.get('http://localhost:3000/checkIt/users', {
    params: { username: $scope.username, password:$scope.password } 
}).success(... same as before);

The request body is undefined simply because you can't pass params through a GET request, you'll probably want to change your route name and use a POST request instead.

By the way, it may be cleaner to move your API requests in a UserService and inject it in your controller because you will eventually need to re-use them somewhere else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM