nagy.zsolt.hun
nagy.zsolt.hun

Reputation: 6694

JavaScript - XMLHttpRequest response status 0

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

Answers (2)

Akhil Sekharan
Akhil Sekharan

Reputation: 12693

Its because it violates the Same origin policy.

Do:

  1. GO to w3School website.
  2. Open developer console.
  3. Paste and run your code and you will get the result

Upvotes: 1

simoncereska
simoncereska

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

Related Questions