Monday, 1 June 2015

Angularjs JSONP

$http.get() and $http.post() and put() and delete() are very commons functions.

However, there is one more function: JSONP, you can get more information from there:
https://docs.angularjs.org/api/ng/service/$http

get() and post() put and delete are using XMLHttpRequest object, JSONP uses JSONP object.

You can get more information about JSONP from there: http://en.wikipedia.org/wiki/JSONP

Flickr feed use JSONP : https://www.flickr.com/services/feeds/docs/photos_public/

https://api.flickr.com/services/feeds/photos_public.gne?tags=apple&tagmode=all&format=json

To call this endpoint, you must specify the format=json.

One more hidden parameter of JSONP is jsoncallback. if you access the endpoint by browser you will see:


You can see it does't return a json blob.  it is a javascript function with a json blob as a parameter.

So, if you call this endpoint with javascript, you need have a function called jsonFlickrFeed.

Angularjs handle this differently:  The name of the callback should be the stringJSON_CALLBACK.

You can do like this:


Or:


Put it into url or into params.

Then do callbacks like .get() and .post()




No comments:

Post a comment