Reputation: 31
I'm trying to write a program in JavaScript where the user enters something into a search bar, and the program returns the top 3 Wikipedia results and a short description of each. I can successfully take a search result and return titles with the following syntax:
articles1.append(x.query.search[0].title);
However, while the MediaWiki site (https://www.mediawiki.org/wiki/API:Parsing_wikitext) leads me to believe that "text" can be used as a parameter similar to "title," the following doesn't return anything:
articles1.append(x.query.search[0].text);
The same goes for "summary" or any of the other parameters listed. I feel like I may be fundamentally misunderstanding how these parameters are supposed to work, but since I can at least successfully grab the requested titles, I feel like I can't be too far off. Any help is appreciated, full code is below:
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 );
}
})
})
})
Upvotes: 1
Views: 74
Reputation: 1110
The Parsing WikiText page you're referring to explains how to parse arbitrary WikiText. It's not relevant here.
You are using the Search API, and may only use the returned properties of that module. In this instance, you might be looking for the snippet
property:
articles1.append( x.query.search[0].snippet );
Look at the Search API documentation for more options.
P.S. you can also use MediaWiki's API Sandbox to play around and test your API requests.
Upvotes: 1