2016-04-22 60 views
0

您好我正在尝试将yelp api集成到我的角度应用程序中。Angularjs与承诺的多个JSONP调用

var params = { 
     callback: 'angular.callbacks._0', 
     location: 'San+Francisco', 
     oauth_consumer_key: 'xxxxxxx', //Consumer Key 
     oauth_token: 'xxxxxxxxxxxx', //Token 
     oauth_signature_method: 'HMAC-SHA1', 
     oauth_timestamp: new Date().getTime(), 
     oauth_nonce: Math.random().toString(36).substring(7), 
     term: 'Itailian', 
     limit: 5 
     }; 
     var consumerSecret = 'xxxxxxxxxxxxxx'; 
     var tokenSecret = 'xxxxxxxxxxx'; 
     function generateSignature(method, yelpURL, params, consumerSecret, tokenSecret) { 
    return oauthSignature.generate(method, yelpURL, params, consumerSecret, tokenSecret, {encodeSignature: false}); 
    } 
    params['oauth_signature'] = generateSignature(method, yelpURL, params, consumerSecret, tokenSecret); 


//Doest work even if I push below call in array and then do $q.all(promisesArra). 

var defered = $q.defer(); 
     $http.jsonp(yelpURL, {params: params}) 
      .success(function (data) { 
      defered.resolve(data); 
      $localStorage[cusine] = data; 
      }) 
      .error(function (error) { 
      console.log('failed', error); 
      }); 
     return defered.promise; 

我想为不同的术语例如多个呼叫:意大利,墨西哥,.. 我可以做一个JSONP呼叫,但不是多个。

我不知道的问题。我想这是因为回调的角度是硬编码的只能是angular.callbacks._0

+0

尝试使用'.then',而不是'.success' – SuperVeetz

回答

0

$http.jsonp已经返回一个承诺,所以没有必要使用$q.defer() 。此外,从v1.4.4开始,$http.success$http.error已被depreciated所取代。相反,使用then()解决您承诺:

$http.jsonp(yelpURL, params).then(
      function (data) { // successcallback     
      $localStorage[cusine] = data; 
      }, function (error) { // errorcallback 
      console.log(error); 
      }); 

这里是一个工作的jsfiddle:http://jsfiddle.net/saarmstrong/hYACX/8/light/