要使用Ajax中的responseXML,你可以按照以下步骤进行操作:
创建一个XMLHttpRequest对象:var xhr = new XMLHttpRequest();
设置onreadystatechange事件处理程序,以便在请求状态更改时接收响应:xhr.onreadystatechange = function() {if (xhr.readyState === 4 && xhr.status === 200) {// 在这里处理响应}};
发送HTTP请求:xhr.open('GET', 'url', true); // 第三个参数为true表示异步请求xhr.send();
在onreadystatechange事件处理程序中,检查readyState和status是否满足成功条件:if (xhr.readyState === 4 && xhr.status === 200) {// 在这里处理响应}
如果成功,可以通过responseXML属性访问响应的XML文档:var xmlDoc = xhr.responseXML;
现在你可以使用XML DOM方法和属性来处理响应的XML文档了:var title = xmlDoc.getElementsByTagName('title')[0].childNodes[0].nodeValue;
注意:在使用responseXML之前,确保服务器返回的是有效的XML文档,并且Content-Type标头被设置为text/xml或application/xml。如果响应不是有效的XML文档或Content-Type不正确,responseXML将为null。