Rodrigo Zurek
Rodrigo Zurek

Reputation: 4575

angular jsonp factory doesn't work

I have this factory in angular, and its returning me a 0 error code always, even tho i can see it fetches from the json origin, can someone help me?

app.factory('docFactory', function($http) {  
  var docFactory = {      
    async: function(page) {
      var url = 'http://example.com/ProviderRequest?&queryString=searchv2&callback=JSON_CALLBACK';     
        var promise = $http.jsonp(url).error(function (response, status) {
          alert(status);
        }).success(function (response, status) {
          alert(status);
        }).then(function (response, status) {
          return response.data;           
      });
      return promise;
    }};
  return docFactory;
});

Upvotes: 0

Views: 1840

Answers (2)

Kamagatos
Kamagatos

Reputation: 884

Your example above is almost right. Just change the callback parameter to

jsoncallback=JSON_CALLBACK

You should have something like that at the end

$http.jsonp(url + '?jsoncallback=JSON_CALLBACK').success(function(data) {
     console.log(data);
})

Upvotes: -1

rtcherry
rtcherry

Reputation: 4880

I ran into this myself. If your JSONP is a function call or something falsy (null, false, undefined) you will encounter this behavior. Take a look at this code from AngularJS HTTP backend for more info (lines 41 - 54 are relevant).

If you are returning a function, you might just need to return true or something after the call.

Edit: After looking at your plunker it seems that your JSONP response is not calling the callback method. The response should be angular.callbacks._0( { ... } ); where { ... } is your object, and angular.callbacks._0 is the value of the callback query parameter in the request.

Upvotes: 2

Related Questions