javascript – Remove Select2 Parameters


I have a problem using the Select2 plugin, I need to make an AJAX request, but I need the final URL to be the following:


And the way it's coming is this:


My code is like this

ajax: {
    url: "/api/user/findbyname/",
    params: {
        contentType: 'application/json; charset=utf-8'
    dataType: 'json',
    data: function (term) {
        return {
            q: term                    
    results: function (data) {


Select2 uses jQuery's ajax under the hood. When you use data it puts the result in the query string [in a GET ; in POST it would go into the request body], just like the original method . If you want this result in the URL itself (ie in the path ), the "natural" solution would be to put it in the url field:

ajax: {
    url: "/api/user/findbyname/" + term,

And leave the data empty. However, I believe that for this you cannot use ajax this way, but make the request explicitly, using query . I did a quick read through the documentation and this example , but I didn't understand 100% how it works, but anyway I suggest starting there. My attempted (untested) solution would be:

query: function(query) {
        url: "/api/user/findbyname/" + query.term,
        contentType: 'application/json; charset=utf-8'
        dataType: 'json'
    }).done(function(data) {
Scroll to Top