无法从$ http jsonp $ q.defer返回数据对象

时间:2015-06-17 14:05:58

标签: angularjs jsonp angular-promise

当我的console.log(getUserData())我得到Promise {$$ state:Object}但是如何从这个返回的$$状态对象中检索实际值,所以输出是[someArray]或{someObject} ..

getUserData = function(){
var defer = $q.defer();
$http.jsonp('http://api.zoopla.co.uk/api/v1/property_listings.js?area='+localData.area_name+'&api_key=xxx&jsonp=someCallback')
 someCallback = function(userData){
     //console.log(userData);
       defer.resolve(userData)
    }
    return defer.promise;
 };
console.log(getUserData())

2 个答案:

答案 0 :(得分:1)

该函数返回promise。所以你需要正确处理它。 您希望异步代码能够同步运行

getUserData = function(){
var defer = $q.defer();
$http.jsonp('http://api.zoopla.co.uk/api/v1/property_listings.js?area='+localData.area_name+'&api_key=xxx&jsonp=someCallback')
 someCallback = function(userData){
     //console.log(userData);
       defer.resolve(userData)
    }
    return defer.promise;
 };

getUserData().then(function (data) {
    console.log(data);
});

更多关于角度定义和承诺:https://docs.angularjs.org/api/ng/service/$q

答案 1 :(得分:0)

try this console.log(userData.data);

OR

$http.get('http://api.zoopla.co.uk/api/v1/property_listings.js?area='+localData.area_name+'&api_key=xxx&jsonp=someCallback';

 then yourService.getUserData().success(function('success',data){
   data
  }).
  error(function(data, status, headers){status, headers});