I am writing a project thesis and have stumbled on an issue which might set my entire project in doubt. So I just want to triple-confirm before I take any further actions.
I have a javascript file on URL1. The javascript code, for simplicity, retrieves information from URL2.
No matter how much I tried this wouldn’t work so I did a Google search and figured out that the same-origin policy might be the reason.
So I ask you again, is this impossible?
this is totally possible, but there are some restrictions to it;
You can access information of URL2 from URL1, if URL2 provides a JSONP way. You’d generate a script tag that loads a js file (url2/information?id=123&callback=mycallback) in which the JSON is put into the specified function (in this case mycallback). The script on URL2 would look like this;
Thus, URL1 needs a mycallback function defined and will get the information from URL2
A different approach would be to utilize XMLHttpRequest Level2 which can go cross-domain.
Another would be to communicate over onmessage/postmessage and have URL2 iframed within the page of URL1. This goes Cross-Origin as well.
There are other technics to make a hole in the same origin policy, like Hashs (but are very limited in comparison to the above technics)