【发布时间】:2019-05-19 02:00:24
【问题描述】:
我正在尝试在我的应用程序中检索 xml 播客提要。我遇到了一个问题,我正在发出的 fetch 请求之一返回的是 html 文档而不是 xml 提要。奇怪的是,如果我将/GET 发送到与 Postman 相同的 url,它会返回我的 rss 提要。它似乎也可以与 SO 上的代码 sn-p 一起使用(示例如下)。
它返回一个html文档的例子可以在this fiddle看到,它的代码与下面的sn-p完全相同。
请问有人能帮我理解为什么会这样吗?我认为它可以通过设置某种请求标头来解决,也许?
更新 奇怪的是,似乎链接的小提琴有时会获取 xml;我认为是因为它缓存了调用。如果您在新的隐身窗口中打开它,它会再次获取 html。
$('#fetchXML').click(function(){
$('#xmlContent').text('Loading ...');
const feedUrl = 'https://cors-anywhere.herokuapp.com/http://feeds.wnyc.org/dearhankandjohn';
const headers = new Headers({
'X-Requested-With':'XMLHttpRequest'
});
fetch(feedUrl, headers)
.then(function(response) {
return response.text();
})
.then(function(xmlText) {
$('#xmlContent').text(xmlText);
})
.catch(function(err){
$('#xmlContent').text(err);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tr>
<td><button id="fetchXML">
Fetch Feed
</button></td>
</tr>
<tr>
<td id="xmlContent"></td>
</tr>
</table>
【问题讨论】:
-
即使在您提到的 JSFiddle(jsfiddle.net/wz4nps83/2) 中,我也收到了 RSS 提要。
-
@VardamanPK 我添加了更新。似乎是缓存的东西
标签: javascript ajax xml rss podcast