Wikipedia API响应中缺少CORS标头"Access-Control-Allow-Origin" [英] CORS header ‘Access-Control-Allow-Origin’ missing from Wikipedia API response
问题描述
我正在尝试使用reactJS中的axios从Wikipedia API中获取数据. 这是我的要求
I'm trying to fetch data from the Wikipedia API with axios in reactJS. This is my get request
axios.get('https://en.wikipedia.org/w/api.php?action=opensearch&search=lol&format=json&callback=?')
.then((response) => {
console.log(response);
})
.catch((error)=>{
console.log(error);
});
我收到此错误:
跨源请求被阻止:同源策略禁止阅读 位于的远程资源 > https://en.wikipedia .org/w/api.php?action = opensearch& search = lol& format = json& callback = ?. (原因:CORS标头"Access-Control-Allow-Origin"缺失).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://en.wikipedia.org/w/api.php?action=opensearch&search=lol&format=json&callback=?. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
然后我将启动脚本更改为:
Then I changed my start script to:
HTTPS=true yarn start
我的服务器在https中启动,但错误仍然存在.我还尝试按照其他线程中的建议将json更改为jsonp,但这似乎也无济于事.
My server started in https but the error still persists. I have also tried changing json to jsonp as suggested in other threads but it doesn't seem to help as well.
推荐答案
您需要将origin=*
添加到Wikipedia API查询参数:
You need to add origin=*
to the Wikipedia API query parameters:
axios.get('https://en.wikipedia.org/w/api.php?origin=*&action=opensearch&search=lol')
.then((response) => {
console.log(response);
})
.catch((error)=>{
console.log(error);
});
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
For background, see the answer at Does Wikipedia API support CORS or only JSONP available?.
这篇关于Wikipedia API响应中缺少CORS标头"Access-Control-Allow-Origin"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!