维基百科 API 响应中缺少 CORS header 'Access-Control-Allow-Origin'
CORS header ‘Access-Control-Allow-Origin’ missing from Wikipedia API response
我正在尝试使用 reactJS 中的 axios 从 Wikipedia API 获取数据。
这是我的获取请求
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);
});
我收到这个错误:
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).
然后我将启动脚本更改为:
HTTPS=true yarn start
我的服务器以 https 启动,但错误仍然存在。我也尝试按照其他线程中的建议将 json 更改为 jsonp,但它似乎也没有帮助。
您需要添加 origin=*
到维基百科 API 查询参数:
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>
有关背景,请参阅 处的答案。
我正在尝试使用 reactJS 中的 axios 从 Wikipedia API 获取数据。 这是我的获取请求
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);
});
我收到这个错误:
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).
然后我将启动脚本更改为:
HTTPS=true yarn start
我的服务器以 https 启动,但错误仍然存在。我也尝试按照其他线程中的建议将 json 更改为 jsonp,但它似乎也没有帮助。
您需要添加 origin=*
到维基百科 API 查询参数:
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>
有关背景,请参阅