【发布时间】:2016-07-29 23:09:59
【问题描述】:
我开始研究Fetch API,在我的沙盒中,我可以从https://api.github.com/users/octocat/gists 获取它,以通过
返回一组 JSON 对象function getGithubGists() {
fetch('https://api.github.com/users/octocat/gists')
.then(function(response) {
response.json().then(function(data) {
console.log('data', data)
})
})
.catch(function(error) {
console.log('Error', error)
})
}
如果我从返回 XML 的私有 API 获取,我需要进行哪些更改?我加了
headers: {
'Accept': 'text/xml'
}
但我不断收到状态代码 0 并且控制台打印未定义的数据。这是因为 fetch 假设响应是 JSON 吗?
此外,在 Chrome DevTools 的网络选项卡中,我可以看到我期待的 XML 响应。
<?xml version="1.0"?>
<psgxml>
<years>
<year>1974</year>
<year>1952</year>
<year>1928</year>
</years>
</psgxml>
我想通过 console.log 打印这个 XML 响应
谢谢!
【问题讨论】:
标签: javascript json xml fetch-api