Can JavaScript interact with a database that lives on a different server?
I.e. there are three computers involved
- A server : which gives an HTML page with JavaScript to
- A client : which runs that page which wants to talk to
- A second server : which sends and receives data to/from the client
Does this violate the Same Origin Policy? If not why not? If so is there a way around it?
My background – I’m a competent computer scientist but I’ve never dealt with client side programming. While answering feel free to use complex ideas but please assume I know nothing about the JavaScript language or the specific policies in place behind client-side programming.
It does violate Same Origin whenever your html comes from server 1 and AJAX requests go to server 2.
Check out Cross Origin Resource Sharing. It is a standard designed to handle exactly this
situation. It is implemented in most modern browsers.
Alternatively you can use JSONP to serve the data, or if you have no control of the second server, use a reverse proxy to proxy requests to the second server through the first.