views:

539

answers:

5

this is what I am trying to do.

  1. I have made a few .html pages with JavaScript code in it and hosted them on a Yahoo server.

  2. Now when a client with a certain browser views these web pages, the JavaScript code uses XMLHTTPRequest to make a connection at h1ttp://localhost:8080/myservlet/servlet1 to read some data.

  3. I know, I want to connect to the web server running on the client's computer if the client has one i.e. I am using localhost in my xmlHTTPRequest.

But this is not working even when a client has a web server running on port 8080. On the client's computer I can access http://localhost:8080/mysevlet/servlet1 and the servlet is running fine, but through the .html page hosted on Yahoo server it does not work.

Anything that I am doing wrong here?

+1  A: 

Isn't this a cross-domain problem?

Jon
+3  A: 

Due to policy restrictions browsers do not allow you to send XMLHttpRequest to domains different than the domain hosting the web page which in your case is Yahoo.

Darin Dimitrov
+4  A: 

Cross-site Scripting

You cannot access what is not on your domain, unless it is a Web Service returning XML or JSONP

geowa4
When I change the localhost to http://<some-other-server>/sample.xml I can correctly read the XML file even when it is not hosted on Yahoo. So I think it is not a cross script problem.
A: 

As others have commented, this doesn't work because of the browser security model.

You might be able to get around this with an entry in your hosts file.

First, assuming your app is on a yahoo.com domain, open your hosts file and add an entry like this

127.0.0.1 mylocalhost.yahoo.com

Then, in your pages, change your AJAX endpoint to http://mylocalhost.yahoo.com/myservlet/serverl1

I've never tested this, so I can't be certain it will work, but it might. If it does work, every user of this page will need to modify their hosts file like above

Note: your hosts file should be located at C:\WINDOWS\system32\drivers\etc\hosts in windows, and at /etc/hosts in *nix

Peter Bailey
I don't think that will work. The host has to be exactly the same for XMLHTTPRequest to work. Pages loaded from http;//www.example.com cannot access http;//foo.example.com, http;//example.com:8080 or https;//example.com. There are some tricks you can play with document.domain and iframes, but they're not pretty.
Tim Sylvester
A: 

The local machine also needs a proxy set up that maps "http://localhost:8080/whatever" to the yahoo pages with your Ajax code. In order for the code to work, you must load it in the browser using the domain same domain that it tries to access.

I'm not sure how to do this with Tomcat (?), but one option is to use Apache to proxy both the Tomcat server and the Yahoo pages into the same location.

In Apache, this looks like:

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
...
<IfModule proxy_http_module>
  ProxyRequests off
  ProxyPass /static    http://yahoo.com/path
  ProxyPass /myservlet http://localhost:8080/myservlet
</IfModule>

You would then load your HTML from localhost/static, and those pages would be able to make AJAX requests to localhost/myservlet.

Tim Sylvester