admin管理员组

文章数量:1426804

My code is:

function loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "results.xml", false);
    xhttp.send();
    myFunction(xhttp);//(this);
}
function myFunction(xml)
{
    var xmlDoc = xml.responseXML;
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(xml.responseText, "application/xml");
    var x;    
    var txt = "";
    var y;
    y = xmlDoc.getElementsByTagName("entryresult");//.childNodes[0];
    x = y[1].getElementsByTagName("title")[0].childNodes[0].nodeValue;
    alert(x);
}

I am using cakephp. My custom.js is located in webroot/js. My xml file results.xml is located at the same folder.

But always got the message:

  • GET XHR http://localhost/cakephp-3-4-3/result/results.xml [HTTP/1.1 404 Not Found 422ms]
  • XML Parsing Error: mismatched tag.Expected: .
  • TypeError: y[1] is undefined[Learn More]

Those errors are from the console (F12).

My code just do that because i cannot go further than the line before the alert.

i think i am trying to read a file from other folder.

My code is:

function loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.open("GET", "results.xml", false);
    xhttp.send();
    myFunction(xhttp);//(this);
}
function myFunction(xml)
{
    var xmlDoc = xml.responseXML;
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(xml.responseText, "application/xml");
    var x;    
    var txt = "";
    var y;
    y = xmlDoc.getElementsByTagName("entryresult");//.childNodes[0];
    x = y[1].getElementsByTagName("title")[0].childNodes[0].nodeValue;
    alert(x);
}

I am using cakephp. My custom.js is located in webroot/js. My xml file results.xml is located at the same folder.

But always got the message:

  • GET XHR http://localhost/cakephp-3-4-3/result/results.xml [HTTP/1.1 404 Not Found 422ms]
  • XML Parsing Error: mismatched tag.Expected: .
  • TypeError: y[1] is undefined[Learn More]

Those errors are from the console (F12).

My code just do that because i cannot go further than the line before the alert.

i think i am trying to read a file from other folder.

Share Improve this question asked Jun 28, 2017 at 21:44 Sandra G.Sandra G. 1291 gold badge4 silver badges17 bronze badges 2
  • 1 Your first issue is you are getting a 404 error, it cannot find your results.xml file. GET XHR http://localhost/cakephp-3-4-3/result/results.xml [HTTP/1.1 404 Not Found 422ms] This means either the results.xml file is not publicly accessible, or it is looking in the wrong location for it (/cakephp-3-4-3/result/results.xml) – AlienHoboken Commented Jun 28, 2017 at 21:51
  • Changed: xhttp.open("GET", "results.xml", false); to: xhttp.open("GET", "../webroot/files/p1/results.xml", false); Now it continued with the following steps. – Sandra G. Commented Jun 28, 2017 at 22:31
Add a ment  | 

1 Answer 1

Reset to default 2

My problem was solved by changing the line: xhttp.open("GET", "results.xml", false); to: xhttp.open("GET", "../webroot/files/p1/results.xml", false);

本文标签: using javascript to open xml fileStack Overflow