要使用Ajax中的responseXML,你可以按照以下步骤进行操作:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
// 在这里处理响应
}
};
xhr.open('GET', 'url', true); // 第三个参数为true表示异步请求
xhr.send();
if (xhr.readyState === 4 && xhr.status === 200) {
// 在这里处理响应
}
var xmlDoc = xhr.responseXML;
var title = xmlDoc.getElementsByTagName('title')[0].childNodes[0].nodeValue;
注意:在使用responseXML之前,确保服务器返回的是有效的XML文档,并且Content-Type标头被设置为text/xml或application/xml。如果响应不是有效的XML文档或Content-Type不正确,responseXML将为null。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: ajax怎么提交整个form表单