使用这个简单的片段:
fetch("https://www.youtube.com/feeds/videos.xml?channel_id=UCAL3JXZSzSm8AlZyD3nQdBA", { mode: "no-cors" })
.then(r => {
console.debug(r);
r.text().then(t => console.debug(t)).catch(console.error);
})
.catch(console.error);
我收到一个空响应(
null
正文,空 url
,status
为零,ok
是 false
),但是当我转到“网络”选项卡时,我可以在“响应”选项卡中看到数据它。我期待 fetch
给我同样的回应。是什么赋予了?我试过添加
credentials: "include"
但它没有任何区别,也不应该,没有它,这个资源应该是可以访问的。
最佳答案
我会回答,因为它帮助了我:)
the request goes through because it's the response headers that control CORS - so you have to get a response to know if CORS is enabled for the site.
You can see the response body in the console because there's no security issues with seeing the response body in the console, as you can just open the page anyway, right.
The fact that it's an RSS feed is irrelevant - youtube clearly don't want browsers that are viewing other sites to directly access their RSS resources. You need to "proxy" the request on your server
关于javascript - Fetch 返回空响应,但 Network 选项卡显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40516835/