跳转至

使用fetch获取XML数据

以前使用XMLHttpRequest来获取的时候,通过读取XHR对象的responseXML可得到XML数据。使用fetch则可以通过以下方式来完成。

fetch("https://www.cnet.com/rss/news/")
    .then(response => response.text())
    .then(str => (new window.DOMParser()).parseFromString(str, "text/xml"))
    .then(data => console.log(data))
效果如图