1
我正在嘗試在JavaScript中編寫一個程序,用戶在搜索欄中輸入內容,程序返回前三個維基百科結果和每個維基百科的簡短描述。我可以成功地採取了搜索結果和下面的語法返回標題:通過MediaWiki API從文章中返回文本?
articles1.append(x.query.search[0].title);
然而,儘管MediaWiki的網站(https://www.mediawiki.org/wiki/API:Parsing_wikitext)使我相信,「文本」可以作爲類似「稱號參數, 「以下不返回任何內容:
articles1.append(x.query.search[0].text);
」摘要「或列出的任何其他參數也是如此。我覺得我可能從根本上誤解了這些參數應該如何工作,但是由於我至少可以成功地獲得所需的標題,所以我覺得我不能太離譜。任何幫助表示讚賞,充分代碼如下:
HTML:
<input type="text" value='' id=test >
<button>Search</button>
<section class='articles1'>
1.
</section>
<section class='articles2'>
2.
</section>
<section class='articles3'>
3.
</section>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
的JavaScript:
$(document).ready(function() {
var articles1 = $('.articles1');
var articles2 = $('.articles2');
var articles3 = $('.articles3');
var input = $('input');
var button = $('button');
var toSearch = '';
var searchUrl = 'https://en.wikipedia.org/w/api.php';
$("button").click(function(e) {
var search = document.getElementById("test").value;
$.ajax({
url: 'http://en.wikipedia.org/w/api.php',
data: { action: 'query', list: 'search', srsearch: search, format: 'json' },
dataType: 'jsonp',
success: function(x) {
articles1.append(x.query.search[0].title);
articles2.append(x.query.search[1].title);
articles3.append(x.query.search[2].title);
}
})
})
})
在您的ajax請求的成功函數中,嘗試將x記錄到控制檯。這將讓你探索對象的形狀,它應該告訴你到底有什麼可用的以及如何訪問它。 –