获取 returns 空响应但“网络”选项卡显示数据

Fetch returns empty response but Network tab shows data

使用这个简单的片段:

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 正文,空 urlstatus 为零,okfalse),但是当我去转到“网络”选项卡,我可以在其中的“响应”选项卡中看到数据。我希望 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