HTML 파일과 동일한 디렉토리에있는 XML 파일에서 데이터를 가져 오려고하는데 데이터가 반환되지 않는 것 같습니다 (나중에 데이터를 렌더링하는 방법을 모르겠습니다) . 다음은 스크립트입니다 (구문 분석하려는 XML 파일의 샘플 레코드와 함께). 어떤 도움을 주셔서 감사합니다!XMLHttpRequest 객체를 사용하여 데이터를 가져올 수 없습니다.
function getFromAndDoThis(r, callback, opt="") {
var okay = true;
var xhr = new XMLHttpRequest();
if (xhr) {
xhr.addEventListener("readystatechange", function() {callback(opt);}, false);
xhr.open("GET", r, true);
xhr.send(null);
}
else {
okay = false;
}
return okay;
}
/* callback function */
function displayCurriculum(which) {
var data = null;
var chapters, docroot;
data = extractXMLData(xhr);
if (data) {
chapters = data.getElementsByTagName("Chapter");
docroot = document.getElementsById("contentarea")[0];
var i = 0;
/* put each chapter and synopsis (if any) in nodes, and put these nodes in the DOM */
for (i=0; i < chapters.length; i++) {
var chapter = chapters[i];
var synopsis = chapter.firstChild;
var div = document.createElement("div");
var pc = document.createElement("p");
var ps = document.createElement("p");
div.setAttribute("id", toString(i));
pc.innerHTML = chapter.innerHTML;
ps.innerHTML = synopsis.innerHTML;
/* nodes are filled. now attach them to the DOM */
div.appendChild(pc);
div.appendChild(ps);
body.appendChild(div);
} // end for
} // end if
else {
alert("No data.");
}
}
XML 레코드 :
<?xml version="1.0"?>
<Curriculum name="algebra">
<Chapter>
Numbers
<Synopsis>
Real numbers, the number line, arithmetic, factoring, decimals, exponents, radicals, and complex numbers.
</Synopsis>
</Chapter>
</Curriculum>
질문에 코드의 첫 번째 기능은 XML을로드를 통해이 작업을 수행 할 수 있습니다 ... 그리고이 질문은 태그되지 않은'jquery' 그래서 JQuery와 답은 적합하지 않습니다. – Quentin