I have this global method in custom service.
/**
* Global service class for making http requests.
*/
app.service('API', function ($http) {
/**
* Process remote POST request to give URL with given params
* @param {String} url
* @param {String} POST params
* @return {JSON} response from server
*/
this.doHttpRequest = function (type, url, params) {
$http({
method: type,
url: url,
data:params,
timeout:2000,
headers: {"Content-Type": "application/json"}
})
.success(function (data, status, headers, config) {
// successful data retrieval
console.log("request success");
console.log("state: "+status);
return data;
})
.error(function (data, status, headers, config) {
// do some stuff
});
};
});
||
Problem is that, if i'm trying to get response from my cotroler method i get always just undefined.
/**
* Test of service
*/
$scope.serviceTest = function () {
var requestParams = {
"token":'test',
"data":{
"test":'test'
}
};
var url = $rootScope.apiBaseUrl + "user/update";
var result = API.doHttpRequest("POST", url, requestParams);
// HERE I GET UNDEFINED
console.log("Result is: " + result);
};
I red about promises but without luck to get data back.
Question is:
How i should modify my code to obtain returned values?
Thanks for any help