简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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