Js/xml/localhost question

I am unable to load some xml. Am using xampp/localhost.

XMLHttpRequest cannot load file:///C:/xampp/htdocs/a-brandRoadmapTest/cd_catalog.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

What is the best way for me to go about this please?
Thx
D

reference it via http//:localhost/a-brandRoadmapTest/cd_catalog.xml - and not via a file:

All right thank you, will go this out

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      myFunction(xhttp);
    }
  }
  xhttp.open("GET", "http//:localhost/a-brandRoadmapTest/cd_catalog.xml", true);
  xhttp.send();
}

D

still get this

XMLHttpRequest cannot load file:///C:/xampp/htdocs/a-brandRoadmapTest/http//:localhost/a-brandRoadmapTest/cd_catalog.xml. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

the URL should be http://localhost/a-brandRoadmapTest/cd_catalog.xml (note the position of the colon)

1 Like

Dormilich thank you! That got me closer to the target. I still get an error message:

XHR failed loading: GET "http://localhost/a-brandRoadmapTest/cd_catalog.xml".

But that might be due to other js factor. Will work on this some more & see if I can fix it. At least now it understands something is supposed to come into that page.
Thx
D

do you load that page via file or http protocol?

Actually am not sure. But i would say that I am using the protocol? I am just learning xml. following a tutorial where the instructors places the xml in the same page. I went to www3 & found some added info.
Trying out the example from their page using this code in the calling file.

<script>
function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
      myFunction(xhttp);
    }
  }
  xhttp.open("GET", "http://localhost/a-brandRoadmapTest/xml/cd_catalog.xml", true);
  xhttp.send();
}
function myFunction(xml) {
  var i;
  var xmlDoc = xml.responseXML;
  var table="<tr><th>Artist</th><th>Title</th></tr>";
  var x = xmlDoc.getElementsByTagName("CD");
  for (i = 0; i <x.length; i++) { 
    table += "<tr><td>" +
    x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
    "</td></tr>";
  }
  document.getElementById("demo").innerHTML = table;
}
</script>

I tried both in the head section of the html file & at the bottom

Now get this error

XMLHttpRequest cannot load http://localhost/a-brandRoadmapTest/xml/cd_catalog.xml. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
firstTry2.html:22 XHR failed loading: GET "http://localhost/a-brandRoadmapTest/xml/cd_catalog.xml".

look into the browser’s address bar.

another question is, where this xml file is really located on your server (felgall made an educated guess for it)

oh i see. file?

file:///C:/xampp/htdocs/a-brandRoadmapTest/firstTry2.html
& most def appreciated Felgall trying!
D

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.