Reputation: 111
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?
Upvotes: 8
Views: 11906
Reputation: 23158
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.
Upvotes: 0
Reputation: 105906
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
Upvotes: 1
Reputation: 41833
You cannot access what is not on your domain, unless it is a Web Service returning XML or JSONP
Upvotes: 6
Reputation: 1039248
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.
Upvotes: 5