CORS header ‘Access-Control-Allow-Origin’ missing from Wikipedia API response [duplicate]

浪子不回头ぞ 提交于 2019-12-11 06:39:23

问题


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);

    });

I got this error:

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

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.


回答1:


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?.



来源:https://stackoverflow.com/questions/47733007/cors-header-access-control-allow-origin-missing-from-wikipedia-api-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!