I am using angular’s $http.jsonp() request which is successfully returning json wrapped in a function:
var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=jsonp_callback";
$http.jsonp(url).
success(function(data, status, headers, config) {
//what do I do here?
}).
error(function(data, status, headers, config) {
$scope.error = true;
});
How to access/parse the returned function-wrapped-JSON?
UPDATE: since Angular 1.6
You must now define the callback like so:
$http.jsonp('some/trusted/url', {jsonpCallbackParam: 'callback'})Change/access/declare param via
$http.defaults.jsonpCallbackParam, defaults tocallbackNote: You must also make sure your URL is added to the trusted/whitelist:
$sceDelegateProvider.resourceUrlWhitelistor explicitly trusted via:
$sce.trustAsResourceUrl(url)success/errorwere deprecated.USE:
Previous Answer: Angular 1.5.x and before
All you should have to do is change
callback=jsonp_callbacktocallback=JSON_CALLBACKlike so:And then your
.successfunction should fire like you have it if the return was successful.Doing it this way keeps you from having to dirty up the global space. This is documented in the AngularJS documentation here.
Updated Matt Ball’s fiddle to use this method: http://jsfiddle.net/subhaze/a4Rc2/114/
Full example: