从HTTP在角工厂返回响应数据 [英] Returning response data from HTTP in Angular factory

查看:155
本文介绍了从HTTP在角工厂返回响应数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这篇关于从HTTP在角工厂返回响应数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆