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

Ano*_*han 10 javascript fetch

代码看起来像这样,不知道如何读取响应数据.任何的想法?.

var url = 'http://www.bbc.co.uk/sport/football';

fetch(url, {
  mode : 'no-cors'
}).then(function(response) {
  console.log(response);
});
Run Code Online (Sandbox Code Playgroud)

响应对象

Bra*_*rad 13

你不能.

如果原点不支持CORS,则无法直接获取响应数据.这就是no-cors......允许您以某种方式使用响应,但实际上并未读取/访问数据.


小智 -5

使用以下其中一项,具体取决于您期望的数据:

response.blob()
response.text()
response.formData()
response.json()
response.arrayBuffer()
Run Code Online (Sandbox Code Playgroud)