0

I have an external Javascript file that is hosted on a server along with a xml file. I have included this file in an HTML page hosted on another server. Now the External JS file has a .$( document ).ready function that has a $.get() function that calls the xml file hosted on the same server , gets some values from it and append them to a div.

Now if my HTML and JS file are hosted on same server the code works perfectly fine and the the values get appended

But in when i host the HTML file on another server and include the JS as an external java script file the values are not getting appended

Is there a way to make the JS work

Hassan Jalil
  • 1,114
  • 4
  • 14
  • 34
  • This has nothing to do with where the JavaScript is hosted. It is entirely about where the XML is hosted. – Quentin Nov 22 '14 at 20:57
  • XML is hosted on the same server as the JS file the HTML file is on another server – Hassan Jalil Nov 22 '14 at 21:07
  • @HassanJalil You can run script from other servers. But you can only establish AJAX connections to the same server of the website, independently on the server where the script came from. – Oriol Nov 22 '14 at 21:13
  • @HassanJalil — Exactly. The XML is from a different origin to the HTML. That's the problem. – Quentin Nov 22 '14 at 21:23
  • and what is the best possible solution to this other than moving the xml ? – Hassan Jalil Nov 22 '14 at 21:42
  • @Quentin currently my get in JS is $.get('advancedsearch.xml', function(data) . Should i provide the full path to the file or will it look up where its hosted , Will it look for it in the folder the JS exists or the folder where the HTML exists ? – Hassan Jalil Nov 22 '14 at 21:44
  • URIs in browser-side JS are relative to the HTML document. So you'll need an absolute URI (and to deal with the same origin policy) – Quentin Nov 22 '14 at 21:46
  • @Quentin and how do we deal with the same origin policy? since XML is hosted on a separate server – Hassan Jalil Nov 22 '14 at 22:05
  • @HassanJalil "*how do we deal with the same origin policy?*". See [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy). – Oriol Nov 22 '14 at 22:15

0 Answers0