javascript - AngularJS Async request method service return data using promise -
i have global method in custom service.
/** * global service class making http requests. */ app.service('api', function ($http) { /** * process remote post request give url given params * @param {string} url * @param {string} post params * @return {json} response 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 info retrieval console.log("request success"); console.log("state: "+status); homecoming data; }) .error(function (data, status, headers, config) { // stuff }); }; }); ||
problem that, if i'm trying response cotroler method 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 undefined console.log("result is: " + result); }; i reddish promises without luck info back.
question is:
how should modify code obtain returned values?
thanks help
modify service doesn't phone call on promise returned $http, notice return.
app.service('api', function ($http) { /** * process remote post request give url given params * @param {string} url * @param {string} post params * @return {json} response server */ this.dohttprequest = function (type, url, params) { homecoming $http({ method: type, url: url, data:params, timeout:2000, headers: {"content-type": "application/json"} }); }; }); then when consume service homecoming promise can run success , error on
api.dohttprequest("post", url, requestparams) .success(function (data, status, headers, config) { // successful info retrieval console.log("request success"); console.log("state: "+status); // info }) .error(function (data, status, headers, config) { // stuff }); javascript ajax angularjs rest
No comments:
Post a Comment