Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Client's 'localhost' from JavaScript Online

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?

like image 331
user139123 Avatar asked Jul 16 '09 15:07

user139123


People also ask

Can I access localhost from Internet?

You are accesing localhost , meaning you have a web server running on your machine. To access it from Internet, you need to assign a public IP address to your machine. Then you can access http://<public_ip>:<port>/ .

How do I access someone else's localhost?

localhost is a special hostname that almost always resolves to 127.0. 0.1. If you ask someone else to connect to http://localhost they'll be connecting to their computer instead or yours. To share your web server with someone else you'll need to find your IP address or your hostname and provide that to them instead.

Can JavaScript interact with server?

JavaScript. Web browsers (aka clients) can also run JavaScript code. This is client-side code that runs on your computer. (It's also possible to run JavaScript on a server, but that's a bit outside the scope of this tutorial.)


2 Answers

Cross-site Scripting

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

like image 109
geowa4 Avatar answered Oct 21 '22 02:10

geowa4


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.

like image 28
Darin Dimitrov Avatar answered Oct 21 '22 03:10

Darin Dimitrov