0

I get this code from w3schools, It work perfect in w3 but when I run the same code in my vscode along with cd_catalog.xml I don't get any output and also I don't have a clear vision on XMLHttpRequest(); who it's work, can any one explain me.

function loadXMLDoc() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xmlhttp.open("GET", "cd_catalog.xml", true);
  xmlhttp.send();
}

function myFunction(xml) {
  var x, i, xmlDoc, txt;
  xmlDoc = xml.responseXML;
  txt = "";
  x = xmlDoc.getElementsByTagName("ARTIST");
  for (i = 0; i< x.length; i++) {
    txt += x[i].childNodes[0].nodeValue + "<br>";
  }
  document.getElementById("demo").innerHTML = txt;
}
<!DOCTYPE html>
<html>
<body>
<h2>My CD Collection:</h2>

<button type="button" onclick="loadXMLDoc()">
Get my CD collection</button>

<p id="demo"></p>

</body>
</html>
  • 2
    **Strongly** recommend using [MDN](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest), not w3schools (which despite the superficial name has nothing whatsoever to do with the W3C), although w3schools is better than it used to be. Also suggest using [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch), not `XMLHttpRequest`, but beware of the API footgun I write about [here](http://blog.niftysnippets.org/2018/06/common-fetch-errors.html). – T.J. Crowder Jul 04 '21 at 15:04
  • Open the developer tools in your browser. Look at the console. See if there are any error messages. (See also [this](https://idownvotedbecau.se/nodebugging/) and [this](https://idownvotedbecau.se/itsnotworking/). I would be surprised if this wasn't a duplicate of https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local – Quentin Jul 04 '21 at 15:05

0 Answers0