tôi đã tìm thấy không thể lấy cả hai mô tả và url cùng một lúc, vì vậy tôi chia làm hai javascript phương pháp, mô tả get đầu tiên, thứ hai get url:
function get_wiki_info() {
$.ajax({
url: 'http://it.wikipedia.org/w/api.php',
data: { action: 'query', list: 'search', srsearch: $("input[name=city]").val(), format: 'json' },
dataType: 'jsonp',
success: function (data) {
console.log('wiki', data.query.search[0].snippet);
$('#info-wiki-text').html(data.query.search[0].snippet);
get_wiki_links();
},
fail: function (data) {
$('#info-wiki-text').html("Impossible retrieve information for " + $("input[name=city]").val());
}
});
}
function get_wiki_links() {
$.ajax({
url: 'http://it.wikipedia.org/w/api.php',
data: { action: 'query', generator: 'allpages', search: $("input[name=city]").val(), format: 'json', gapfrom: $("input[name=city]").val(), gapto: $("input[name=city]").val(), prop: 'info', inprop: 'url' },
dataType: 'jsonp',
success: function (data) {
console.log('wiki', data.query.pages);
$.each(data.query.pages, function (key, val) {
$('#wiki-city-link').attr('href', val.fullurl);
});
},
fail: function (data) {
console.log(data);
}
});
}
Nếu bạn thích, để lấy mô tả :
https://it.wikipedia.org/w/api.php?action=query&list=search&srsearch=Your%20Params&utf8=
để lấy url:
https://it.wikipedia.org/w/api.php?action=query&generator=allpages&search=Your%20Params&gapfrom=Your%20Params&gapto=Your%20Params&prop=info&inprop=url&utf8=
Nguồn
2017-08-14 09:01:28
Bạn nói đúng nhưng tôi dự kiến các URL trong phản hồi XML | JSON: nếu wikipedia sẽ thay đổi cấu trúc URL (it.wiki .../wiki /) trong tương lai thì sao? có URL trực tiếp từ tính bảo mật của url wikipedia API. Nhưng không có vấn đề và cảm ơn cho câu trả lời của bạn. –
Thực ra, bạn có thể làm điều đó, xem phần chỉnh sửa của tôi. – svick
Thực ra, nó không chỉ là mã hóa URL. So sánh: https://en.wikipedia.org/wiki/пиетет với đối tác được mã hóa URL của nó: –