Before you mark this as a duplicate, please read carefully..
I am using select2 to select remote data via ajax.
I am able to console.log and see the returned data, but the response is not displayed using on the select2 options.
I am searching movie titles from the “themoviedb” api to display them on the select field as autocomplete suggestions.
Any help would be appreciated
<html>
<head>
<title>Movie</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css" rel="stylesheet" />
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js"></script>
</head>
<body>
Search Movie Title: <select class="search-movie" style="width:300px;"></select>
<script>
$(".search-movie").select2({
minimumResultsForSearch: 10,
ajax: {
delay: 250,
url: 'http://api.themoviedb.org/3/search/movie',
dataType: 'json',
contentType: "application/json",
type: 'GET',
data: function (params) {
var query = {
api_key : 'fe4d662718177607da792694a52e7911',
query: params.term
}
return query;
},
processResults: function (data) {
return {
results: $.map(data.items, function (item) {
return {
text: item.original_title,
id: item.id
}
console.log(results);
})
};
}
},
});
</script>
</body>
</html>
2
Answers
Using Easy autocomplete, I was able to replicate the same functionality
I have tried your code, the problem lies in the way that you are mapping the data.The response is sent in results key where you are accessing the item key. Changing the $.map(data.items) to $.map(data.results) would do the trick