-1
链结https://en.wikipedia.org/api/rest_v1/#/我想用自己的REST API
我在我的节目一个搜索按钮来获得来自维基数据。用户输入他必须搜索的文本,然后编程从维基媒体中获取该页面。我怎么能在原生JavaScript中做到这一点。
链结https://en.wikipedia.org/api/rest_v1/#/我想用自己的REST API
我在我的节目一个搜索按钮来获得来自维基数据。用户输入他必须搜索的文本,然后编程从维基媒体中获取该页面。我怎么能在原生JavaScript中做到这一点。
这是我前段时间使用fetch写的示例代码。
function fetchRandomContent() {
fetch('https://en.wikipedia.org/w/api.php?format=json&origin=*&action=query&generator=random&rvprop=content&prop=revisions', {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
credentials: "same-origin"
}).then(function(promise) {
promise.json().then(function(response) {
//--- Parsing Wikipedia data ---//
var data = {},
keys = Object.keys(response.query.pages),
key = keys[0];
data.title = response.query.pages[key].title
data.content = response.query.pages[key].revisions[0]["*"]
//--- Parsing Wikipedia data ---//
});
}, function(error) {
console.log('shit!', error);
});
}
fetchRandomContent();
您可以参考网址https://www.mediawiki.org/wiki/API:Main_page为维基媒体
的API细节