【发布时间】:2017-12-02 10:35:42
【问题描述】:
我的代码是:
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);
}
我正在使用 cakephp。我的 custom.js 位于 webroot/js 中。我的 xml 文件 results.xml 位于同一文件夹中。
但总是得到消息:
- 获取 XHR http://localhost/cakephp-3-4-3/result/results.xml [HTTP/1.1 404 未找到 422ms]
- XML 解析错误:标签不匹配。预期:。
- TypeError:y[1] 未定义[了解更多]
这些错误来自控制台 (F12)。
我的代码只是这样做,因为我不能比警报之前的行更进一步。
我想我正在尝试从其他文件夹中读取文件。
【问题讨论】:
-
您的第一个问题是您收到 404 错误,它找不到您的 results.xml 文件。
GET XHR http://localhost/cakephp-3-4-3/result/results.xml [HTTP/1.1 404 Not Found 422ms]这意味着 results.xml 文件不能公开访问,或者它正在寻找错误的位置 (/cakephp-3-4-3/result/results.xml) -
更改:xhttp.open("GET", "results.xml", false); to: xhttp.open("GET", "../webroot/files/p1/results.xml", false);现在它继续执行以下步骤。
标签: javascript xml cakephp load edit