Returning response data from HTTP in Angular factory

霸气de小男生 提交于 2019-12-10 05:42:13

问题


.factory('Api', function($http) {
         var API = "http://127.0.0.1:4567/";
         return {
             get: function(method) {
                 return $http.get(API + method).success(function(result) {
                     return result;
                 });
             }
         }
     }

Then

console.log(Api.get("MAppData"));

Returns

Object {then: function, success: function, error: function}

Why does it not return the result (response data)?


回答1:


$http returns a promise and you need to chain .then() to get the data like this:

Api.get("MAppData").then(function(response){
    var data = response.data;
});


来源:https://stackoverflow.com/questions/19121918/returning-response-data-from-http-in-angular-factory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!