1. 使用XMLHttpRequest
这种方法主要涉及XMLHttpRequest的两个属性responseType与response。在调用XMLHttpRequest实例的open方法之后并且在调用send方法之前,将responseType的属性值设置为字符串"document",然后当响应成功后XMLHttpRequest实例的response属性值便是一个Document实例,但是如果响应类型不匹配的话属性值则是null
示例
1 let xml = new XMLHttpRequest(); 2 3 xml.onload = () => { 4 5 if (xml.status >= 200 && xml.status <= 299) { 6 7 console.log(xml.response); 8 9 } 10 11 }; 12 13 xml.open("GET", "/"); 14 15 xml.responseType = "document"; 16 17 xml.send();
2.使用DOMParser对象
示例
let doc = (new DOMParser).parseFromString("需要将其解析为Document的字符串", "text/html");