Getting cross domain Ajax to work!

Last week I was working on a project which required me to use ajax to fetch the results from 3 different servers and use the result together to have a comparison side-by-side.

I had a little knowledge of Ajax, so I had to strive a bit for getting things to work. Here, I share my findings.

 

Problem: Using Ajax to fetch cross domain data.

What does it mean?

Suppose my current domain is blahblah.com and I want something from phewphew.com then I need to do a cross domain fetch with Ajax.

By conventions Javascript doesn't allow to fetch such cross domain data. It involves security issues. Anyways, I didn't go much into that !!

Quick solution 1 :

One solution to this problem is to use a custom servlet ( or php, whatever you use, it will act as a proxy ) /JSP page that downloads the JSON ( or any other type ) and simply reproduces it.  In that case, you will put your Ajax calls to a page on same domain and that will work perfectly fine.

 

Solution 2 :

Another solution, if using JQueries, using JSONP would help retrieving JSON data easily.

 

NOTE: These were really abstract solutions, I would try to illustrate it with example code soon .