json - AngularJS $http REST call returns null data (SOLVED) -
i have rest service returns json object. trying make authentication responses empty data.
i did notice call asychronous , when user pressing login button makes call before getting username , password. decided use $q constructor in order fix it, problem consists, still returns null data.
what doing wrong?
thanks in advance.
factory
angular.module('myapp', ['ngroute']) .factory('user', ['$http', '$q', function($http, $q) { return { login: function(username, password) { var deferred = $q.defer(); $http.post('http://localhost:8080/cashinrestservices_war/rest/user/login', {username: username, password: password}) .then (function(data, status, headers, config){ deferred.resolve(data); }, function(data, status, headers, config) { deferred.reject(data); }) return deferred.promise; } } }])
controller
.controller('mainctrl', ['$scope', 'user', function($scope, user) { $scope.username = "viewer"; $scope.password = "viewer"; $scope.login = function() { user.login($scope.username ,$scope.password) .then(function(response) { console.log("success!"); $scope.status = response.status; $scope.data = response.data; $scope.username = response.username; alert("success!!! " + json.stringify({data: response.data})); }, function (response) { $scope.data = response.data || "request failed"; $scope.status = response.status; console.log("error!!!"); alert( "failure message: " + json.stringify({data: response.data})); }) }; }])
*****edit*****
i did change code little bit. think problem how $http written.
factory
angular.module('myapp', ['ngroute']) .factory('user', ['$http', function($http) { return { login: function(username, password) { return $http({method:'post', url: 'http://localhost:8080/cashinrestservices_war/rest/user/login', username: username, password: password}) } } }])
it did somehow worked returns logincheck:false. seems not recognize correct username , password.
response = object {data: object, status: 200, config: object, statustext: "ok"}
log:
object {data: object, status: 200, config: object, statustext: "ok"}config: objectheaders: objectmethod: "post"paramserializer: (b)password: "viewer"transformrequest: array[1]transformresponse: array[1]url: "http://localhost:8080/cashinrestservices_war/rest/user/login"username: "viewer"__proto__: objectdata: objectlogincheck: false__proto__: objectheaders: (c)arguments: (...)caller: (...)length: 1name: ""prototype: objectconstructor: (c)__proto__: object__proto__: ()<function scope>closureclosureglobal: windowstatus: 200statustext: "ok"__proto__: object__definegetter__: __definegetter__()__definesetter__: __definesetter__()__lookupgetter__: __lookupgetter__()__lookupsetter__: __lookupsetter__() constructor: object()hasownproperty: hasownproperty()isprototypeof: isprototypeof()propertyisenumerable: propertyisenumerable()tolocalestring: tolocalestring()tostring: tostring()valueof: valueof()get __proto__: __proto__()set __proto__: set __proto__()
i figured out. login function causing problem $scope.login = function()
used $event object.
html
<div><button ng-click="login($event)" type="submit">login</button></div>
factory
angular.module('myapp', ['ngroute']) .factory('user', ['$http', function($http) { return { login: function(username, password) { // return $http({method:'post', url: 'http://localhost:8080/cashinrestservices_war/rest/user/login', username: username, password: password}) var data = {username: username,password: password}; console.log(json.stringify(data)); return $http({ method:'post', url: 'http://localhost:8080/cashinrestservices_war/rest/user/login', data: json.stringify(data), headers: {'content-type': 'application/json'} }) } } }])
controller
.controller('mainctrl', ['$scope', 'user', function($scope, user) { $scope.username = "viewer"; $scope.password = "viewer"; $scope.login = function(event) { event.preventdefault(); user.login($scope.username ,$scope.password) .then(function(response) { $scope.status = response.status; $scope.data = response.data; alert(json.stringify({data: response.data})); }, function (response) { $scope.data = response.data || "request failed"; $scope.status = response.status; alert( "failure message: " + json.stringify({data: response.data})); }) }; }])
Comments
Post a Comment