Remove Select2 parameters

3

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

api/user/findbyname/name

And the way it's coming is this:

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

My code is this way

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);
    }
}
    
asked by anonymous 19.03.2014 / 16:52

2 answers

2

Select2 uses ajax of jQuery beneath the cloths. When you use data it puts the result in the query string [in GET ; no POST it would go to the request body], such as the original method . If you want this result in the URL itself (i.e. in path ), the "natural" solution would be to put it in the url :

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

And leave data empty. However, I believe that for this you can not use ajax in this way, but rather make the request explicitly, using query . I gave a quick read in the documentation and in this example , but I did not understand 100% how it works, but anyway I suggest starting there. My attempt at a solution (not tested) 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);
    });
}
    
19.03.2014 / 17:01
-1

This plugin comes by default with get method which causes this in URl, changing by post method I imagine the problem solves, but it's just a guess.

 ajax: {
 type: 'POST',
 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);
 }
 }
    
19.03.2014 / 17:10