在 codesandbox 上使用 React 获取:跨源错误

Fetch with React on codesandbox : cross-origin error

我在 codesandbox 上的 React 应用程序中有以下获取调用: https://codesandbox.io/s/react-fetch-example-for-so-vxxie

如果我激活任一线路

.then(console.log(data))

loadOptions={this.state.data}

我得到: 抛出了跨源错误。 React 无法访问开发中的实际错误对象。详情请见 https://fb.me/react-crossorigin-error。

为什么以及如何解决这个问题?

(请注意我在 fb.me 前面添加了一个 space 以通过 Whosebug 此处的验证)

不确定错误消息究竟来自哪个宇宙,但 .then(console.log(data)) 无法工作,因为 data 未定义。你宁愿

.then(data => console.log(data))

下一个 .then 仍然会失败,因为它需要上面的结果来执行它的任务,所以你会这样做,这在你的沙箱示例中对我有用:

.then(data => { console.log(data); return data; })