使用 xhr.responseXML; 

通过这个属性正常获取XML或HTML文档对象有两个前置条件: 

1. Content-Type头信息的值等于: text/xml 或 application/xml

2. xhr.responseType 需要赋值为: "document"

var xhr = new XMLHttpRequest();
xhr.open('GET', '/server', true);

xhr.responseType = 'document';
xhr.overrideMimeType('text/xml');

xhr.onload = function () {
  if (xhr.readyState === 4 && xhr.status === 200) {
    console.log(xhr.responseXML);
  }
};

xhr.send(null);

 

注意: 如果Content-Type不等于 text/xml 或 application/xml, 那需要通过xhr.overrideMimeType('text/xml') 强制进行XML解析.

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2022-12-23
  • 2022-01-01
  • 2022-01-05
  • 2022-12-23
猜你喜欢
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案