javascript – Remove Select2 Parameters

Question:

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:

api/user/findbyname/name

And the way it's coming is this:

api/user/findbyname/?q=name&_=1395243972884

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) {
        console.log(data);
    }
}

Answer:

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) {
    $.ajax({
        url: "/api/user/findbyname/" + query.term,
        contentType: 'application/json; charset=utf-8'
        dataType: 'json'
    }).done(function(data) {
        console.log(data);
    });
}
Scroll to Top
AllEscort