从HTTP在角工厂返回响应数据工厂、数据、HTTP

2023-09-13 03:26:51 作者:Party 派对

.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;
        });
    }
}

}

然后

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

返回

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

为什么它没有返回结果(响应数据)?

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

推荐答案

$ HTTP 返回一个承诺,你需要链。那么() 来获取数据是这样的:

$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;
});