views:

162

answers:

2

Hello, for one reason or another I need for javascript to access a webserver on the localhost. This localhost webserver is under our control so we can have whatever software running in it.

How would you do this? I've seen things like YQL but this accesses another domain from the internet. This kind of access causes a lot of problems with firewalls and such. So I want to access the same computer that the browser is running on.

How would you do this with javascript and whatever software running on the localhost server?

Also, the javascript is being run from an internet site. And the localhost server will not be running on the same port are the internet website is.

Is this possible to do? I know about the cross-domain restrictions but I've also seen there are ways around them such as YQL. How does something like YQL work? How would you reimplement it?

A: 

Since it's a different port, the security restrictions for cross domain access will apply.

You can use the getJSON call in jQuery for cross domain access:

http://api.jquery.com/jQuery.getJSON/

Here's an IBM tutorial of how it's done via the jQuery call mentioned above (with JSONP):

http://www.ibm.com/developerworks/library/wa-aj-jsonp1/

If you are using IE, you can quickly put together a workaround by setting the site you are trying to access from script to your trusted sites, setting security to the "Access data sources across domains" option.

Gurdas Nijor
We'd prefer to not have to do such setup as putting it in trusted sites and also need it to be cross-browser
Earlz
Added the cross browser solution.
Gurdas Nijor
+1  A: 

You could try something like JSONP.

In JSONP, you use dynamically created script tags to access things in other domains. The URL can be used to pass data to the server, and the returned script can call a callback to return data. As far as I remember, there are no special restrictions on making requests to localhost (unlike the filesystem), but I might be wrong.

Matti Virkkunen
So for accessing localhost JSONP isn't even required? Is localhost different from other domains?
Earlz
What I meant was that (AFAIK) localhost isn't treated any different from *Internet* domains. Cross-domain policy still applies.
Matti Virkkunen