javascript - Fetch 返回空响应,但 Network 选项卡显示数据

标签 javascript fetch-api

使用这个简单的片段:

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

关于javascript - Fetch 返回空响应,但 Network 选项卡显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40516835/

相关文章:

javascript - 关节.shapes.uml.js : Uncaught TypeError: Cannot read property 'Generic' of undefined

javascript - 使用 fetch.catch 时控制台出现错误

node.js - 创建 react 应用程序/ express 代理错误 : Could not proxy request (ECONNRESET)

fetch-api - Zapier POST出现错误 "body used already for"

Aurelia:获取客户端响应没有我的数据

javascript - fetch的缓存需要客户端管理吗?

javascript - jquery ajax 调用完成后如何推迟默认表单操作?

javascript - 使用 Handlebars.js 渲染本地模板

javascript - TinyMCE - 如何使用 onClick 而不是 <a href =""> 作为链接?

javascript - 获取分页的下一个偏移量 Sequelize