I'm trying to set up a factory for multiple requests, but the result is always undefined
, what could be wrong?
My angularjs:
(function(){
angular
.module("store", ['ui.bootstrap', "ngAnimate", "ngSanitize"])
.controller("homeCtrl", appController)
.factory('httpService', function($http, $q){
return {
getData: getDataHandler
};
function getDataHandler(url, data){
$http({
method: 'POST',
url: url,
data: {'p': data}
}).then(handlerSuccess, handlerError);
};
function handlerSuccess(response){
return response.data
}
function handlerError(response){
if(!angular.isObject(response.data) || !response.data.message){
return $q.reject('Ops! Aconteceu algo errado.');
}
return $q.reject(response.data.message);
}
});
function appController($scope, $http, httpService){
console.info(httpService.getData('php/list.php', null));
$scope.page = httpService.getData('php/list.php', null);
}
})();
My example list:
<?php
$p = json_decode(file_get_contents("php://input"))->p;
$list = '{
"title": "Aulas",
"categorias": [{
"nome": "Matemática",
"subcategorias": [
"Algebra",
"Estatística",
"Cálculo I",
"Cálculo II",
"Cálculo III",
"Geometria Analítica"
]
},{
"nome": "Engenharia",
"subcategorias": [
"Materiais",
"Estrutural",
"Elétrica",
"Química"
]
}]
}';
print($list);