在Angular工厂中从HTTP返回响应数据

时间:2022-01-23 20:14:28
.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 个解决方案

#1


9  

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

$ http返回一个promise,你需要链接.then()来获取这样的数据:

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

#1


9  

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

$ http返回一个promise,你需要链接.then()来获取这样的数据:

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