JSONP read as JSON with padding is used for the crossdomain communication from the client(Browser). This method however should have the server side compatibility.We should pass a callback function as a parameter to the url and this should taken care by the server. For example a Twitter Search API has the url as
http://search.twitter.com/search?q=cwcSo now if u need the result for the above search in json format we have the following
http://search.twitter.com/search.json?q=cwc11Inorder to get the above jsondata back to the client we need to add a callback function parameter to the above url, The function parameter name is server dependent and as per twitter its 'callback'
now we rewrite the url as
http://serach.twitter.com/search.json?q=cwc11&callback=?We have some jQuery ready made method to get the data once you pass the above url. There is a method called as getJSON taht supports jsonp. All you need is to pass the url and the call back function to getJSON()
so for our twitter example you can just do this
$.getJSON(url,callbackfucntion(data){
//handle the data
});So far so good, You form the url pass it to getJSON you get back the data from another domain without the help of your server. Now what happened in the background ?
The JSONP works with the help of script tag injection When you do the above it just forms a script tab in your html page where the scr of the scrip tag is nothing but the url.
hmm now JSONP solves the problem of crossdomain GET. Can we use the same to solve crossdomain POST.
The answer for the above question is NO. This is just because the jsonp works with the help of script tag injection and POST ing data becomes impossible with script injection.
No comments:
Post a Comment