Search code examples
angularjsjsonp

AngularJs and jsonp not working


I have some problems with jsonp and AngularJs

I one page, i have to make about 15 json calls, each one by jsonp

My calls look like this :

$http.jsonp("http://www.example.com/feed1.json?callback=JSON_CALLBACK1")

And in my json feed, the data is wrapped by JSON_CALLBACK1

But i always obtain this error :

Uncaught ReferenceError: JSON_CALLBACK1 is not defined

Now i noticed in the network tab in the Google Chrome inspector that the call was :

http://www.example.com/feed1.json?callback=angular.callbacks._0

So i changed my json file, to wrap data with angular.callbacks._0(); instead, and it worked for this one, but it did not work for all my json

I started wraping all my jsons with angular.callbacks._1, angular.callbacks._2, angular.callbacks._3 ... byt i noticed that the number isn't always the same ? and if the number is a two digits, it doesn't work. For example, i tryed angular.callbacks._15, and i always obtain this error

Uncaught ReferenceError: angular.callbacks._15 is not defined

So is there a way to fix all those jsonp problems once for all with AngularJs ?

Thanks


Solution

  • The callback=angular.callbacks._123 parameter is an information for the server that the client (browser) expects the response to be wrapped inside a function named angular.callbacks._123 like so:

    angular.callbacks._123({
     "key": "value"
    });
    

    You should change your server code to inspect the parameter value and if present use it as the wrapping function name.

    You can find more details in wikipedia.