Thursday, July 16, 2009

YQL rocks

A few days ago, I was doing research on how to perform cross domain ajax. I was hitting a lot of problem as this method (as in cross domain) was not supported by xhr. This object will definitely hit into a problem of security as you are trying to do a cross domain ajax. However, after google around, I found that jquery has a plugin named flxhr. This plugin claims that it can do a cross domain. I read the documentation, it says that it uses flash as a proxy to do request. But, it needs a configuration file in the machine to be planted. I dont think, it is a good way to implement my plugin. Hence, I leave away this plugin.

That time, I really gave up to find a way on using xhr. It is definitely not the right way to get data from another domain. However, I found a way to fetch data from different domain. We can fetch data from domain using technique called JSONP. This technique is actually a script to create or inject another script dynamically. The destination website needs to provide a service in JSON format and wrap it in a function. By then, we can do JSONP. As I need to deal with twitter and I see twitter provides service in JSON format. Then, I can definitely use this way.

Again, all the things do not go as smooth as what I expected. Some of the provided services need basic authentication. Arrgghh, again, I cant use JSONP for this. Feel so damn curious on how to resolve this problem. Upon googling around (again, I dont want to use this word, actually :P). I found out about a new service provided by yahoo. This service is called as YQL. It stands for yahoo query language. It acts like a proxy for any open webservice provided by well known website such as facebook and twitter. Also, this service provides you a way on how to modify header of the request using javascrip. Finally, I can fetch data from twitter using the great YQL.

Here is the snippet of my javascript to use YQL webservice.

No comments: