Wikipedia API响应中缺少CORS标头"Access-Control-Allow-Origin" [英] CORS header ‘Access-Control-Allow-Origin’ missing from Wikipedia API response

查看:132
本文介绍了Wikipedia API响应中缺少CORS标头"Access-Control-Allow-Origin"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆