简体   繁体   English

从Angular工厂的HTTP返回响应数据

[英]Returning response data from HTTP in Angular factory

.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)? 为什么不返回结果(响应数据)?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM