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

时间:2022-12-04 16:10:34

when i console.log(getUserData()) i get Promise {$$state: Object} but how can i retrieve actual value from this returned $$state object so the output be [someArray] or {someObject}..

当我的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 个解决方案

#1


1  

That function returns promise. So you'll need to handle it properly. You expect async code to behave syncronously

该函数返回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);
});

More about angular deffered and promises: https://docs.angularjs.org/api/ng/service/$q

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

#2


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

#1


1  

That function returns promise. So you'll need to handle it properly. You expect async code to behave syncronously

该函数返回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);
});

More about angular deffered and promises: https://docs.angularjs.org/api/ng/service/$q

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

#2


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