0

I am trying to load a 3rd party xml document using JQuery/Javascript, but without success:

alert("Before");
$.ajax({
    type: "GET",
    url: "www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml",
    dataType: "xml",
    success: function(xml) {
            alert("OK");
    }
});
alert("After");

The "OK" box is not displayed, but the xml is available with a browser. This code example is available at JSFiddle.

How is one supposed to load a 3rd party XML in Javascript?

durron597
  • 31,968
  • 17
  • 99
  • 158
Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453
  • 3
    possible duplicate of [Loading cross domain html page with jQuery AJAX](http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-jquery-ajax) – durron597 Sep 16 '15 at 18:43
  • Also, you need the protocol for the url (http://) –  Sep 16 '15 at 18:49

3 Answers3

2

The protocol has to be specified, http:// (or perhaps https://).

url: "http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml",

Updated code: http://jsfiddle.net/gv9Kr/1/
As you can see, the code does not work, because of the Same-origin policy.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • It still does not work with 'http'. I guess I should have this xml on my own website. – Jérôme Verstrynge Jan 18 '12 at 21:11
  • @JVerstry You have a few options: 1) A server-side proxy, 2) Another data-exchanging format, such as JSONP (if it's supported by the server). – Rob W Jan 18 '12 at 21:12
  • @JVerstry I just looked for you, there's no JSONP existing support for the service. You have to use a custom server-side proxy. – Rob W Jan 18 '12 at 21:17
  • Ok. Thanks, I fiddled a solution with a PHP file loading the xml. My javascript code loads the php file from my site. – Jérôme Verstrynge Jan 18 '12 at 22:40
2

It is due to cross domain restrictions. There are plenty of resources available in the internet just google on it. There are various work arounds to it one of which is YQL

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124
1

same origin policy prevents you from doing that. you must find ways to circumvent this. for JSON type data, there is JSONP. here's a question from SO that might be related to your issue.

Community
  • 1
  • 1
Joseph
  • 117,725
  • 30
  • 181
  • 234