Reputation: 6694
I would like to fetch data from an XML file to a JS variable (I would like to use pure JS, no jQuery). But I have always get error while downloading the file:
var url = "http://www.w3schools.com/xml/note.xml";
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = function(event){ processRequest(event,xmlhttp); };
xmlhttp.send();
function processRequest(event,xmlhttp) {
if(xmlhttp.readyState != 4) return;
alert("status: " +xmlhttp.status);
}
The response xml is always empty - the response status is 0.
Upvotes: 2
Views: 3809
Reputation: 12693
Its because it violates the Same origin policy.
Do:
Upvotes: 1
Reputation: 3043
Try local url. Your code doesn't match same origin policy
p.s. w3schools is not where you want to learn, mdn and dochub.io ;)
Upvotes: 1