How to append params to ajax request in Select2?
Asked Answered
V

1

5

i'm building a web app using Laravel, and i have to implement tag selection, like this one used by stackoverflow, loading options via ajax and if is not exist create it, i did choose Select2 jquery plugin, the problem i have with it, is cant get it to append parameters to the ajax url,

Route :

 /tags/{tag}

how can i append the term of select to my url ?

Veldaveleda answered 3/11, 2014 at 13:49 Comment(0)
F
7

In Select2 3.x, you can pass a function as the ajax.url option. It will be passed the current search term as the first parameter, which sounds like what you are looking for.

$element.select2({
   ...
    ajax: {
        url: function (term) {
            return '/tags/' + term;
        },
        ...
    }
});
Filar answered 3/11, 2014 at 14:33 Comment(4)
thanks it works, but there is another parameter also appended automatically tags/search/{term}?_=1415027037653 i dont know where this number come from ??Veldaveleda
That's a cache-busting token that is added by jQuery. If you set cache: false in your ajax configuration, it should prevent it from being added.Filar
no doesn't work ! i have set it to false with no luck!Veldaveleda
My bad, you should set it to true. The default, for Select2, is false.Filar

© 2022 - 2024 — McMap. All rights reserved.