I am facing an issue in retrieving POST data in an Angular.js project. This is the factory I am using:
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 trying to retrieve 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 evident from my code, there are multiple console.log calls. However, when I run the application, I only see the following output:
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 displayed in mtapp.app.js:52 should be the same as mtapp.controller.js:10. But the object from the factory seems to have a value of 200 only when viewed in the log...
My goal is to extract the value from the JSON in the factory (GeneratedKey) and assign it to the controller (in the $scope.key).
What could possibly be going wrong here? :(