Jorge had the right idea. $ http.jasonp is the easiest route.
Here is an example of using $ q to return a pending promise.
function getForecast( lat, lon ){
var deferred = $q.defer();
var apiKey = 'my-actual-api-key-here';
var url = 'https://api.forecast.io/forecast/' + apiKey + '/' + lat + ',' + lon + '?callback=JSON_CALLBACK';
$http.jsonp( url )
.success( function forecastReceived( forecastData, status, headers, config ) {
deferred.resolve( forecastData );
} )
.error( function forecastFailed( errorData, status, headers, config ) {
deferred.reject( errorData );
} );
return deferred.promise;
}
or you can use (as I did) restangular, but first you need to configure it:
function isDataGood( forecastData ){
var isGood = false;
return isGood;
}
var apiKey = 'my-actual-api-key-here';
Restangular.setBaseUrl( 'https://api.forecast.io/' );
Restangular.setJsonp( true );
Restangular.setDefaultRequestParams( 'jsonp', { callback: 'JSON_CALLBACK' } );
var API = Restangular.one( 'forecast/' + apiKey );
function getForecast( lat, lon ){
var deferred = $q.defer();
function failed( error ){
deferred.reject( error );
}
API
.one( lat + ',' + lon )
.get()
.then(
function forecastReceived( forecastData ) {
if( isDataGood( forecastData ) ){
deferred.resolve( forecastData );
} else {
failed( { msg : 'ERROR : received bad data' } );
}
},
function forecastFailed( error ) {
failed( error );
} );
return deferred.promise;
}
source
share