使用JS中的fetch检索外部XML文件

时间:2018-01-09 01:59:40

标签: javascript jquery xml fetch-api

我尝试使用Javascript从下面的URL检索XML数据,然后解析我想要的数据。我在下面设置了获取请求。我的浏览器显示我收到了yield.xml文件,但我无法访问它。相反,我得到错误:

scripts.js:149 Uncaught (in promise) TypeError: Cannot read property 'getElementsByTagName' of undefined
    at scripts.js:149
(anonymous) @   scripts.js:149

任何帮助将不胜感激



function yield() {
  var url = "https://www.treasury.gov/resource-center/data-chart-center/interest-rates/Datasets/yield.xml";
  var myHeaders = new Headers();
  myHeaders.append('Content-Type', 'text/plain');
  fetch(url, {
    headers: myHeaders,
    mode: "no-cors",
  }).then(function(response) {
    console.log(response);
    var xmlDoc = response.responseXML;
    var bc1month = xmlDoc.getElementsByTagName("BC_1MONTH").nodeValue;
    console.log(bc1month);
  });
}




Image of network tab showing yield.xml file and contents

0 个答案:

没有答案