1

I'm trying to use JSONP with Restangular. I'm using the Songkick API and when making a GET request I'm receiving no response data from a different domain but locally I receive data no problem.

I've created the following Factory for configuring Restangular and a controller. I'm a little unsure about how to use setDefaultRequestParams. Maybe the configuration is incorrect?

angular
    .module('ModuleName')
    .factory('RestFactory', Factory);

function Factory (Restangular) {
    var factory = {
        songkick: songkick
    };

    return factory;

    function songkick () {
        return Restangular.withConfig(function(RestangularConfigurer) {
            RestangularConfigurer.setJsonp = true;
            RestangularConfigurer.setDefaultRequestParams('jsonp', {
                callback: 'JSON_CALLBACK'
            });

            RestangularConfigurer.setDefaultRequestParams('get', {
                reason: 'attendance',
                apikey: 'xxxxxxxxxx'
            });

            RestangularConfigurer.setBaseUrl('http://api.songkick.com/api/3.0/');
            RestangularConfigurer.setRequestSuffix('.json');
            RestangularConfigurer.setDefaultHttpFields({cache: true});
        });
    }

}


angular
    .module('ModuleName')
    .controller('UserController', Controller);

function Controller ($stateParams, RestFactory) {
    var user = this;

    activate();

    function activate () {
        RestFactory.songkick().one('users/'+$stateParams.username+'/calendar')
            .get()
            .catch(function(response){
                console.log("Error with status code", response.status);
            });
    }

}
Adam
  • 209
  • 4
  • 12
  • Figured out that I just needed the params set for jsonp and setJsonp is a method which should have been called with true as an argument. – Adam Jul 05 '15 at 19:07

1 Answers1

4

Did you miss the JSONP option settings for Restangular? Something like the following:

//JSonp
RestangularProvider.setJsonp(true);
RestangularProvider.setDefaultRequestParams('jsonp', {callback: 'JSON_CALLBACK'});

Documented here

mephisto
  • 41
  • 2