简体   繁体   English

带有参数的angularjs $ http.post

[英]angularjs $http.post with parameters

I'm new at angular and if my question is kinda low lvl dont be angry with me. 我是新手,如果我的问题有点低,请不要生我的气。

I have web service which returns sessionId and login success message if user will pass auth. 我有Web服务,如果用户将通过身份验证,它将返回sessionId和登录成功消息。 for example url is that: 例如url是:

http://localhost:8181/login?username=USERNAME&password=12345 HTTP://本地主机:8181 /登录用户名=用户名和密码= 12345

and here's my response: 这是我的回应:

{"sessionId":"0997cec2a8b34c84ba8419ab1204e6aa","loginSucceeded":true}

here's my login controller: 这是我的登录控制器:

app.controller('loginCtrl', function($scope, loginService){
    $scope.login=function(user){
        loginService.login(user);
    }
});

and here's my service: 这是我的服务:

app.factory('loginService', function($http){
    return{
        login: function(user){
            var $promise=$http.post('http://localhost:8181/login?', user);
            $promise.then(function(msg){
                if(msg.loginSucceeded=="true")
                    console.log("opa")
                else
                    console.log("den");
            });
        }
    }
});

and I have user.username and user.password in my scope (using textboxes). 并且我的范围内有user.username和user.password(使用文本框)。

How can I pass those parameters in url and how can I parse that response? 如何在url中传递这些参数,以及如何解析该响应?

In your code you're passing the username and password in the URL of the POST request. 在您的代码中,您要在POST请求的URL中传递用户名和密码。 If that's what you really want (it's more common to pass them as POST data) than you can use this: 如果那是您真正想要的(通常将它们作为POST数据传递),则可以使用以下命令:

login: function(user){
    var url = 'http://localhost:8181/login?username=' + user.name + '&password=' + user.password;
    $http.post(url).then(function(msg){
        if(msg.loginSucceeded==="true"){
            console.log("opa")
        }else{
            console.log("den");
        }
    });    
}

If you want to pass the data as POST data, you can pass that as the second argument in the $http.post() call: 如果要将数据作为POST数据传递,则可以将其作为$http.post()调用中的第二个参数传递:

login: function(user){
    var url = 'http://localhost:8181/login';
    var data = {username: user.name, password: user.password};
    $http.post(url, data).then(function(msg){
        if(msg.loginSucceeded==="true"){
            console.log("opa")
        }else{
            console.log("den");
        }
    });
};

I never seen anyone passing login data via query string, if you are in simple http protocol... you should consider using Basic Access Authentication or oAuth ... 我从未见过有人通过查询字符串传递登录数据,如果您使用的是简单的http协议...则应考虑使用基本访问身份验证oAuth ...

by the way, if you need to do what described above... this could be help you! 顺便说一句,如果您需要执行上述操作,这可能会对您有所帮助!

 angular .module('test', []) .service('LoginService', function($q, $http) { var self = this; self.login = function(username, password) { var configs = { cache: false }; var payload = { "username" : username, "password" : password }; // The Method Post is generally used with a payload, but if you need to pass it as query string... you have to do: configs.params = payload; return $http .post('/api/login', null /* but normally payload */, configs) .then(function(result) { console.log('LoginService.login:success', result); return result.data; }) .catch(function(error) { console.log('LoginService.login:error', error); return $q.reject(error); }); ; }; }) .controller('LoginCtrl', function(LoginService, $scope) { var vm = $scope vm.username = 'hitmands'; vm.password = 'helloWorld'; vm.debug = 'CIAO'; vm.onFormSubmit = function(event, form) { if(form.$invalid) { event.preventDefault(); return; } vm.debug = null; return LoginService .login(vm.username, vm.password) .then(function(data) { vm.debug = data; }) .catch(function(error) { vm.debug = error; }) ; }; }) ; 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <article ng-app="test"> <div ng-controller="LoginCtrl"> <form ng-submit="onFormSubmit($event, loginForm);" name="loginForm"> <input type="text" placeholder="username" ng-model="username"> <input type="password" placeholder="Password" ng-model="password"> <div> <button type="submit">Send Login Data</button> </div> <div style="color: blue; padding: 1em .5em;" ng-bind="debug | json"> </div> </form> </div> </article> 

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

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