Sandra G.
Sandra G.

Reputation: 129

using javascript to open xml file

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:

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.

Upvotes: 0

Views: 11025

Answers (1)

Sandra G.
Sandra G.

Reputation: 129

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

Upvotes: 2

Related Questions