javascript - 为什么我的请求正文为空?

标签 javascript angularjs node.js mongodb express

我最近开始学习MEAN堆栈,遇到了一个问题。我正在尝试从 Angular 向本地服务器发送 $http.get 请求,但请求正文未定义。奇怪的是,它在我的帖子中并没有未定义。我知道问题可能出在正文解析器上,我花了几个小时试图解决它,但没有成功。感谢您的帮助!

这是我的快速代码:

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);

这是我的登录 Controller ,其中有我的获取请求:

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')
    }

}]);

最后,我不知道这是否有帮助,但这是我执行 $http.post 请求的代码片段

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

最佳答案

不会有 GET 的主体!生活就是这样。没有 GET 的正文。现在来解决问题。您希望在服务器端使用 req.query 来访问这些值。

在 Angular 方面,您需要对代码稍作更改(在 url 中发送密码是一件坏事):

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

关于javascript - 为什么我的请求正文为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30409682/

相关文章:

javascript - 是否可以使用带有字符串的变量来标识另一个变量?

angularjs - Karma + Jasmine 下 angular.mock.inject 的 TypeScript AngularJS Controller 测试问题

mysql - 为订阅系统设计一个作业调度程序

unit-testing - 如何在 Angular 单元测试中从指令而不是 Controller 模拟范围

node.js - Angular-cli aot 构建在生产模式下失败

node.js - Facebook Messenger bot 未按顺序发送消息

javascript - 根据数字范围更改正文背景

javascript - 非常基本的 React + NodeJS 获取 : Why is my response not what I want?

php - 通过php中的函数更新变量值

jquery - 根据变量在 Accordion 标题上设置背景颜色