I have a POST data to retrive in a Angular.js project. This is my factory:
angular.module('mtApp').factory('getKey', function($http) {
return {
getData: function(data) {
var key='';
return $http({
url: '../php/key_gen.php',
method: "POST",
headers: {'Content-Type': 'application/json'}
}).success(function (data) {
console.log(data); //value is right as expected
return data;
})
.error(function (data, status, headers, config) {
console.log('Erro : ' + status + ' ' + headers);
});
}
}
});
The way I´m getting the data is:
$scope.key = 'ok';
getKey.getData()
.success(function($scope,data){
$scope.key = data.GeneratedKey;
console.log(data.GeneratedKey); //undefined
console.log(data); //200 o.O
});
console.log($scope.key); //still 'ok' O.o
As you can see in my code I have several console.log calls. When I run the application the only thing I see is:
mtapp.controller.js:13 ok
mtapp.app.js:52 Object {GeneratedKey: "d1bc7a5e840a6c24d87b90dde9e075de1f0e3b34978ca9f319…69d839b4e2a004e1f8d728939867afe189cfb8848c6a8ee38"}
mtapp.controller.js:9 undefined
mtapp.controller.js:10 200
The value in line mtapp.app.js:52 should be the same as mtapp.controller.js:10. But the object from the factory has only a value of 200 when I try to view in the log...
My objective is get the value from the JSON in the factory (GeneratedKey) to the controller (in the $scope.key).
What am I doing wrong? :(
.success(function(data,$scope){?.success(function(data, statusCode){console.log($scope.key); //still 'ok' O.ostill printing only 'ok'.