Saturday, March 12, 2011

How JSONP works.


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=cwc

So now if u need the result for the above search in json format we have the following

http://search.twitter.com/search.json?q=cwc11

Inorder 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.

Sunday, February 6, 2011

IP address of Virtual OS

Recently i was stuck up in a place where i installed my VirtualBox ad Linux in windows xp. I tried pinging the Virtual linux from another win XP machine. But I was not able to do it.After a long googling found that there is a small tweak.

Go to Virtual Linux settings and check out for the network mode. There are different networks modes, change your network mode to bridged instead of NAT..

The reason you could't ping with NAT is because, this network mode assigns the virtual Linux a private IP and hecnce it cant be accessed from outsite.

Once you change that from NAT to Bridged you are all set.

Happy Virtualization :)