How to load a XML from a 3rd party site with Javascript/JQuery?

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?

Upvotes: 0

Views: 550

Answers (3)

Joseph
Joseph

Reputation: 119847

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.

Upvotes: 1

ShankarSangoli
ShankarSangoli

Reputation: 69905

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

Upvotes: 2

Rob W
Rob W

Reputation: 349012

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.

Upvotes: 2

Related Questions