Skip to content

Instantly share code, notes, and snippets.

@psycalc
Forked from camelcc/WikiSearchSuggestion
Created July 1, 2016 18:09
Show Gist options
  • Save psycalc/61364276b56171709cd2d4c122319a6f to your computer and use it in GitHub Desktop.
Save psycalc/61364276b56171709cd2d4c122319a6f to your computer and use it in GitHub Desktop.
A search suggestion using jQueryUI autocomplete + Wikipedia API.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>jQuery demo - wikipedia suggestion</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
</head>
<body>
<div class="input-widget">
<input id="wiki-input" />
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
<script>
// On document ready
$(function() {
$("#wiki-input").autocomplete({
minLength: 1,
autofocus: true,
delay: 100,
source: function( request, response ) {
$.ajax ({
url: 'http://en.wikipedia.com/w/api.php?action=opensearch&format=json&search=' + request.term,
dataType: 'jsonp',
contentType: 'application/json; charset=utf-8',
type: 'GET',
async: true,
success: function(wikidata) {
console.log('jquery ajax returned for search ' + request.term + ' in wikipedia');
response(wikidata[1]);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
response("");
}
})
}
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment