Fra*_*ssi 1 javascript mediawiki wikipedia fetch mediawiki-api
我正在开发一个维基百科查看器,我正在尝试从维基百科API中提取一些数据.这应该是一个正常的请求,任何想法为什么这个方法没有给出任何响应?我正在使用fetch库.行console.log(数据)根本不运行.
function getArticleList() {
var searchFor = "";
searchFor = document.getElementById('intext').value;
console.log(searchFor);
fetch("https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchFor + "&limit=5").then(function(resp) {
console.log("trySearch");
return resp.json()
}).then(function(data) {
console.log(data);
document.querySelector.artName.innerText = data.object[1];
document.querySelector.textArt.innerText = data.object[0];
document.querySelector.href = data.object[2]
})
};
Run Code Online (Sandbox Code Playgroud)
来自Wikimedia的文档:'对于匿名请求,原始查询字符串参数可以设置为*,这将允许来自任何地方的请求.
以下对我有用:
fetch("https://en.wikipedia.org/w/api.php?&origin=*&action=opensearch&search=Belgium&limit=5").then(function(resp) {
console.log(resp);
return resp.json()
}).then(function(data) {
console.log(data);
Run Code Online (Sandbox Code Playgroud)
请注意,我用"比利时"填写了我自己的搜索,但您的代码应该使用正确的修改.