如何使用模式'no-cors'的fetch API获取数据

How to get data using fetch API with mode 'no-cors'?

本文关键字:API 获取 数据 fetch 何使用 no-cors 模式      更新时间:2023-09-26

我的代码是这样的:如何读取响应数据?

var url = 'http://www.bbc.co.uk/sport/football';
fetch(url, {
  mode : 'no-cors'
}).then(function(response) {
  console.log(response);
});
响应对象

你不能。

如果原点不支持CORS,则实际上无法直接获得响应数据。这就是no-cors的全部意义……允许您以某些方式使用响应,但不实际读取/访问数据。

根据您期望的数据使用以下其中一个:

response.blob()
response.text()
response.formData()
response.json()
response.arrayBuffer()