Wednesday, July 8, 2009

Ajax Cross Site

Now, I am trying to develop a Confluence plugin which needs an Ajax cross site. This plugin is actually need a data from outside. It means that I need to open and maybe hold the connection during the data fetching process. If I do this in server side code, it will burden the server a lot. Thus, I need to distribute the burden among the client. Thus, Confluence will not be complained to be slow anymore.

Upon googling around, it seems that it is impossible. A lot of people hit into a security problem. As xhr (XmlHTTPRequest) does not allow any script crossing site. However, I was finding clue which reveal the script of doing cross site. JSONP!! Yeay... By using JSONP technique, it is possible to fetch data from outside using javascript. The idea of JSONP is actually to have a dynamic insertion on function. As you may know that we are allowed to import a script from outside. For example:
< type="text/javascript" src="http://vs1.pbworks.com/shared/statics/packed-v70340828.js">

But, the application which you want to fetch data from, should provide a way so that the json which is a return value of rpc, wrap the return value in certain function. The following is a piece of code which does JSONP:

var surl = baseurl + "&callback=?"
$.getJSON(surl, function(data) {
var res = '<>The data: '+data.screen_name+''
$("#result").html(res)
})

No comments: