我尝试从启用跨源的 API 中获取一些数据,但出现此错误。使用他们提供的示例
与JSONPlaceholder(用于测试的在线 REST API)相同:
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
两个请求(JSONPlaceholder 和我的 API)都可以使用 Insomnia(一个 REST 客户端)正常工作,所以我猜测问题出在我的 React 应用程序(16.13.1)中。
编辑
经过一些测试,似乎只有在从 a 调用 fetch 函数时才会发生错误<form>
,以下是一些详细信息:
handleSubmit = () => {
fetch('https://jsonplaceholder.typicode.com/todos/1')
.then(response => response.json())
.then(json => console.log(json))
}
<form onSubmit={this.handleSubmit} >
<button type="submit">FETCH</button>
</form>
谢谢。