本节要解决的问题
异步加载并解析 XML 文件。
核心要点
- 创建 XMLHttpRequest 对象
- onreadystatechange 处理结果
- responseXML 得到文档对象
var xhttp = new XMLHttpRequest();\nxhttp.onreadystatechange = function () {\n if (this.readyState == 4 && this.status == 200) console.log(this.responseXML);\n};\nxhttp.open("GET", "note.xml", true);\nxhttp.send();现在更常用 fetch + DOMParser,思路是一样的。
本文根据 W3School XML 教程 的知识点重新整理、补充示例编写,非原文转载。