Cross domain scripting would be such a big issue especially in year 2011, I never imagined. What I had envisioned to be a small problem turned out to be much bigger and complicated than I thought.
Let us go over the problem statement first
I have application 1 http://www.skill-guru.com making a json request to http://ww.geekevalaution.com.
While it works in IE(I was working with IE6) , it will fail in Firefox (I was using FF 3.6)
From Mozilla docs
Cross-site HTTP requests are HTTP requests for resources from a different domainthan the domain of the resource making the request. For instance, a resource loaded from Domain A (http://domaina.example) such as an HTML web page, makes a request for a resource on Domain B (http://domainb.foo), such as an image, using the
imgelement (http://domainb.foo/image.jpg). This occurs very commonly on the web today — pages load a number of resources in a cross-site manner, including CSS stylesheets, images and scripts, and other resources.
Or another example
http:/127.0.0.1:8080/myservice making call to http:/127.0.0.1:8090/myservice2
will also be not allowed in FF because even on same server and different port number , it considers it cross domain scripting.
Cross-site HTTP requests initiated from within scripts have been subject to well-known restrictions, for well-understood security reasons. For example, prior to Firefox 3.5, HTTP Requests made using the
XMLHttpRequestobject were subject to the same-origin policy. In particular, this meant that a web application using
XMLHttpRequestcould onlymake HTTP requests to the domain it was loaded from, and not to other domains. Developers expressed the desire to safely evolve capabilities such as
<a rel="internal" href="https://developer.mozilla.org/en/XMLHttpRequest">XMLHttpRequest</a>to make cross-site requests, for better, safer mash-ups within web applications
What will happen when you make a request, and observer in Firebug with FF, you could that the response is blank
The headers would look something like
How do we fix cross domain scripting issue ?
The simple solution is to allow the server to which request is being made to server request to any domain or to a list of domains. The important thing to remember is that the changes are to be made in the server which is serving the web service.
There are multiple ways to do it
1. You change settings in your apache’s httpd-vhosts.conf file ( I am using Apache 2.2 )
CustomLog “logs/skg1-access.log” common
Header set Access-Control-Allow-Origin “*”
Options -Indexes FollowSymLinks
AllowOverride AuthConfig FileInfo
Allow from all
JkUnmount /*.jsp ajp13
Now after you set the value in apache server and look at the header and would see
HTTP/1.1 200 OK
Date: Mon, 01 Dec 2008 00:23:53 GMT
Keep-Alive: timeout=2, max=100
which essentially means your web service application’s server is serving request to any caller. In the next post I have talked about Cross domain scripting solution in Tomcat