success is not a function when using axios as transport

176 views
Skip to first unread message

Bhushan Nagaraj

unread,
Nov 13, 2018, 10:19:51 PM11/13/18
to select2
Hi all,

I am new to javascript, callback functions and select2. Thanks in advance for your help :)

I am looking at implementing select2 to search against an API but I will have to use axios instead of the default jQuery method. Below is my code. I am able to send and retrieve results but I am not sure how to use the success callback.

I get "TypeError: success is not a function"

$("#profile-select").select2({
ajax: {
transport: function(params, success, failure){
var req = axios.post("/rest/vue/1.0/profile/search", {query: $("#profile-select").val()})
.then(function(response){
success(response);
})
.catch(function(error){
alert(error);
});
},
processResults: function(data){
var processedArray = [];
data.profiles.forEach(function(item){
processedArray.push({id: item.ID, text: item.name});
});
return processedArray;
}
},
minimumInputLength: 2,
placeholder: "Select a profile",
allowClear: true
});

Questions

1. How do I return the response data to processResults in the .then function on the axios request?
2. What is the best way to pass the input from the select list to the post request? Currently I am using jQuery.val() function which doesn't seem to work.
Reply all
Reply to author
Forward
0 new messages