Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

The problem is, I don't know how to access/parse the returned function-wrapped-JSON. Any guidance is much appreciated. Thanks!

share|improve this question
2  
With JSONP you don't "access/parse the returned function-wrapped-JSON." Your callback is called; it receives the JSON data as an argument. – Matt Ball Aug 22 '12 at 3:39
I've tried doing something like – akronymn Aug 22 '12 at 13:59
(sorry hit enter too soon above) At what point is my callback called? A code snippet would be really helpful. I've tried a number of different things at this point and am stumped. – akronymn Aug 22 '12 at 14:21
The callback is called when the response comes back. Do you have a function named jsonp_callback? If not, there's your problem. – Matt Ball Aug 22 '12 at 14:24
for now I've written a simple function to just return the first element of the json, function jsonp_callback(data) { return data.found; //should be 3 } – akronymn Aug 22 '12 at 14:32

2 Answers

up vote 3 down vote accepted

This should work just fine for you, so long as the function jsonp_callback is visible in the global scope:

function jsonp_callback(data) {
    // returning from async callbacks is (generally) meaningless
    console.log(data.found);
}

var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=jsonp_callback";

$http.jsonp(url);

Full demo: http://jsfiddle.net/mattball/a4Rc2/ (disclaimer: I've never written any AngularJS code before)

share|improve this answer
That did it! It turns out the scope that I was messing up. Thank you! – akronymn Aug 22 '12 at 16:31
6  
Please select the 2nd answer below since it goes inline with the AngularJS docs. – matsko Jan 9 at 19:45
This answer was not very helpful. It doesn't follow the scope of AngularJS. – xil3 May 27 at 19:17
@xil3 thanks for the feedback; unfortunately only the OP (akronymn) can switch the accepted answer, not me. – Matt Ball May 27 at 19:47

All you should have to do is change callback=jsonp_callback to callback=JSON_CALLBACK like so:

var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";

And then your .success function 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.

Updated Matt Ball's fiddle to use this method: http://jsfiddle.net/subhaze/a4Rc2/114/

Full example:

var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";

$http.jsonp(url)
    .success(function(data){
        console.log(data.found);
    });
share|improve this answer
2  
This is the definitely the correct answer. Saved me a ton of time and messy code. Thank you! – urban_racoons Jun 5 at 3:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.