import React from 'react';
class FetchData extends React.Component{
state = {}
setStateAsync(state) {
return new Promise((resolve) => {
this.setState(state, resolve)
});
}
async componentDidMount() {
const url="https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Stack%20Overflow"
const res = await fetch(url,{mode:'no-cors'})
const data = await res.json()
await this.setStateAsync({data:data})
}
render(){
return (
<div>
{this.state.data}
</div>
);
}
}
export default FetchData
我正在尝试制作一个简单的维基百科查看器应用程序。我检查了网络并尝试使用简单的提取功能,但这个错误似乎仍然存在。
答案 0 :(得分:0)
将模式设置为&#39; no-cors&#39;在你的提取不解决CORS问题,它只是忽略它们。 根据Wikipedia API文档,您可以添加&amp;&amp; origin = *&#39;对您提出未经身份验证的CORS请求的请求(see here)。
最后,您需要使用空的“数据”来启动您的州。对象,你必须在渲染方法中对对象进行字符串化(你不能在React渲染方法中将对象作为子对象)。