【发布时间】:2016-09-16 22:40:30
【问题描述】:
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 when attempting to fetch resource. 捕获底部的错误到目前为止,在尝试使用 fetch() 进行不同操作后,我无法从 API 访问任何数据
任何帮助将不胜感激!
该项目在 codepen 上:http://codepen.io/javascriptisscary/pen/RazKWB
【问题讨论】:
-
如果您在运行请求时打开控制台,您将看到浏览器记录的实际网络错误。它与 CORS 有关,与您的代码无关。
-
stackoverflow.com/questions/37106041/… 维基百科不支持 CORS,请使用 JSONP
标签: javascript fetch wikipedia-api