Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have the following code which make seperate requests for jsonp data. In the code "doRequestA" works fine and returns a result. The issue I have is I need to catch any errors if they occur. I have tried implementing this in "doRequestB", but only receive the alert error (I have ommitted the callback from doRequestB).

Here is the fiddle http://jsfiddle.net/a4Rc2/417/

function jsonp_callback(data) {
    alert(data.found);
}

function jsonp_example($scope, $http) {
    $scope.doRequestA = function () {
        var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=jsonp_callback";
        $http.jsonp(url);
    };

    $scope.doRequestB = function () {
        var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts";
        $http.jsonp(url)
            .success(function (data) {
            alert(data.found);
        }).error(function (data, status, headers, config) {
            alert('error');
        });
    };
}

Any advice is greatly appreciated, thanks in advance.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

You actually are using $http.jsonp incorrectly in both cases. You just can't see the error in the first case because you are not handling it.

With Angular.js's $http.jsonp method, the callback method is handled automatically. You shouldn't use your own methods in the result string, but rather insert JSON_CALLBACK (exactly as written) into your string. This way, you can handle the response using the promise returned from Angular. If you watch the network activity (say, using Firebug or the developer tools in your browser of choice), you'll see JSON_CALLBACK replaced with something like angular.callbacks._0*.

In the second example, you don't have a callback method defined at all, so the result will always error. There's actually no way for the jsonp result to be handled, since it simply returns the JSON object without a callback method, and the result just is ignored.

Here's a working result: http://jsfiddle.net/tPLaN/1/

The code:

function jsonp_callback(data) {

    alert(data.found);
}


function jsonp_example($scope, $http) {
    $scope.doRequestA = function() {

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

        $http.jsonp(url).success(function(data) {
            jsonp_callback(data);
        });
    };   


    $scope.doRequestB = function() {

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


        $http.jsonp(url)               
         .success(function (data) {

             alert(data.found);

        }).error(function (data, status, headers, config) {

             alert('error');         
        });
    };       
}

The only thing I changed was

  1. Correcting the two URLs.
  2. Moving the callback handler on the first method inside the .success() method on the promise.

Believe it or not, the need for JSON_CALLBACK is in the documentation for $http.jsonp, but it's sort of hidden.


* Note, please do not use the replacement for JSON_CALLBACK for anything. It's a private method generated by Angular, I am just showing it to help make more sense of what is happening.

share|improve this answer

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.