简体   繁体   English

angularjs http发布方法

[英]angularjs http post method

Trying to post something back to the mongodb however its not sending anything, when I click submit its passing {} back to the cmd and the network console is hanging on pending then it will fail when its taking long to post. 尝试将某些内容回发到mongodb上,但是它没有发送任何内容,当我单击将其传递的{}提交回cmd且网络控制台挂起挂起时,则需要花费很长时间才能发布。

Can someone shed a light on this one please, thanks. 有人可以帮忙介绍一下这个吗,谢谢。

Html: HTML:

<input type="text" ng-model="vm.user">
<input type="text" ng-model="vm.pass">

Service: 服务:

function _postUser(user,pass){
        var params = {
            user: user,
            pass:pass
        }
        return $http({
            method: 'POST',
            url: '/loginDB',
            params: params
        })
    }

Get users: I get the users from the DB. 获取用户:我从数据库获取用户。

vm.getUsers = function (){
        homeService.getUsers()
            .then(function(response){
                vm.users = response.data;
                console.log(vm.users);
            });
    }

Post action: 动作后:

vm.postUser = function() {
        // console.log('send it back')
        homeService.postUser(vm.user)
            .then(function(response){
                console.log('send it back')
            })
    }

Server.js app.post back to db Server.js app.post回数据库

app.post('/loginDB', function (req, res){
console.log(req.body);
});

Edit: its posting but now taking the ng-model, I know something is wrong with the ng-model but just can't get my head on it. 编辑:它的发布,但现在采用ng-model,我知道ng-model出了点问题,但是无法理解。

db.loginDB.insert(req.body, function(err, doc){
    res.json(doc);
})

Try changing params key to data 尝试将params键更改为data

 return $http({
        method: 'POST',
        url: '/loginDB',
        data: params
    })

In the html too you have the same model for both the inputs, 在html中,两个输入的模型也相同,

controller declaration, 控制器声明,

vm.newuser = {user:'',pass:''};

html html

<input type="text" ng-model="vm.newuser.user">
<input type="text" ng-model="vm.newuser.pass">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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