Sunday, November 18, 2012

basics of JSONP


what is it : It is JSON with some paddind
ex: JSON: {"name","sreenath"}
     JSONP: my({"name","sreenath"})
     in the above 'my ( )' is padding
Why[Usage]: To over come problem of same domain origin policy restriction of AJAX [ you cant make a AJAX call to different domain]. 

How it works:

Its all possible with one condition. < script > tag can access resources in different domain

1.You make request to cross domain resource using script tag passing call back function name.
    < script src="htt://mydomain.com/resource?callback=mycallbackfunc"/>
2. Make your server side code to return passed function with data.
     mycallbackfunc(JSON DATA) ex: mycallbackfunc({"name","sreenath"})
3. implement your call back funcion in your client side code
function   mycallbackfunc(data){
// write your logic here 
}

That is it you have done cross domain request.


Limitations:
1. You can not make POST request with this
2. server side code needs to be written to return JSONP data