user899119
user899119

Reputation: 549

Getting data from localhost service with javascript

I am working on a website that needs access to local resources, like RFID reader and some other hardware. I was thinking about writing a simple web server that would return data I need.

So that web site would access this data with JavaScript, and handle it further.

I know I cannot simply access localhost with XMLHTTPRequest due to same-origin policy limitations.

So is there a simple solution for this? I don't know would if I can access let say localhost.mysite.com if I set it locally to 127.0.0.1? Or maybe JSONP? Or CORS? I am not familiar with these technologies, so I am asking how would you solve this problem, or what would be the easiest way to do this.

Upvotes: 1

Views: 897

Answers (1)

mahemoff
mahemoff

Reputation: 46489

You're probably best off making a packaged Chrome app to do something like this. You will then be able to run it as a regular website experience, but with the right "permissions" setting in the manifest, it will be able to access any domain, including localhost.

It would require anyone using it to "install it", which is just a one-click operation.

Alternatively, if you can control the localhost servers, you can have them output JSONP or the correct CORS headers. Basically, if you want to communicate between any two domains (which includes localhost) on the regular web (ie not including special web apps like Chrome's packaged apps), they both need to provide consent for it.

Upvotes: 1

Related Questions