在 Chrome devtools 控制台中运行 javascript fetch 时出错

问题描述

根据 this SO post,我应该能够从 Chrome devtools 控制台运行获取请求。事实上,如果我点击帖子答案中的按钮,它会起作用。

但是,如果我尝试在控制台中执行完全相同的代码,则不起作用。

fetch('https://jsonplaceholder.typicode.com/posts/1')
  .then(res => res.json())
  .then(console.log)

enter image description here

缺少什么?

解决方法

不知道为什么,但这取决于浏览器的当前域:

主页:

enter image description here

浏览一个页面(任何页面)后:

enter image description here

我的猜测是,在第一种情况下,没有已建立的源域,但更好的错误描述会有所帮助。