this is what I am trying to do.
I have made a few .html pages with JavaScript code in it and hosted them on a Yahoo server.
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.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?
this is what I am trying to do.
I have made a few .html pages with JavaScript code in it and hosted them on a Yahoo server.
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.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?
Share Improve this question edited Jul 16, 2009 at 20:36 user139123 asked Jul 16, 2009 at 15:46 user139123user139123 1111 gold badge2 silver badges5 bronze badges 7- 4 it doesn't work, because it shouldn't. – SilentGhost Commented Jul 16, 2009 at 15:48
- Why is this voted down? Seems like a valid question to me. – MattC Commented Jul 16, 2009 at 15:53
- In the future, please post with more specific question titles. I've edited this one for you. – Peter Bailey Commented Jul 16, 2009 at 15:53
- 1 @MattC take a look at the first version of the question, before the edits – Sinan Ünür Commented Jul 16, 2009 at 16:08
- thanks for the revision. The reason I had h1ttp because, I could't post more than 1 hyperlink. – user139123 Commented Jul 16, 2009 at 20:34
5 Answers
Reset to default 6Cross-site Scripting
You cannot access what is not on your domain, unless it is a Web Service returning XML or JSONP
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.
Isn't this a cross-domain problem?
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
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.