fetch('https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json')
.then(
function(response) {
if (response.status !== 200) {
console.log('Looks like there was a problem. Status Code: ' + response.status);
return;
}
// Examine the text in the response
response.json().then(function(data) {
console.log(data);
});
}
)
.catch(function(err) {
document.write('Fetch Error :-S', err);
});
我使用的获取地址列在这里:https://www.mediawiki.org/wiki/API:Main_page在简单的样本下。目前,它在尝试获取资源时使用 TypeError: NetworkError 捕捉到了底部的错误。
到目前为止,在使用 fetch() 尝试了不同的东西之后,我一直无法从 API 访问任何数据
如有任何帮助,我们将不胜感激!
该项目在 codepen 上:http://codepen.io/javascriptisscary/pen/RazKWB
最佳答案
问题是由于 fetch()
使用 CORS。但是,当我更改为 mode: "no-cors"
时,我不再被跨源请求阻止,而是状态代码为 0。
根据谷歌的文档:https://developers.google.com/web/updates/2015/03/introduction-to-fetch?hl=en
“'no-cors' 旨在向没有 CORS header 的其他来源发出请求并导致不透明的响应,但如前所述,目前在 window 全局范围内这是不可能的.”
因此目前要访问维基百科 api,必须使用与 fetch()
关于javascript - 使用维基百科 API 的 fetch() 结果为 "TypeError: NetworkError when attempting to fetch resource.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37333573/