Define your web method like this:
[System.Web.Services.WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string GetClientList(string searchTerm)
{
return @"{""employees"": [{ ""firstName"":""John"" , ""lastName"":""Doe"" }]}";
}
And provide URL in ajax call like
url: '<%= ResolveUrl("~/trial.aspx/getResults") %>'
so make ajax call in .Select2() as below:
ajax: {
url: '<%= ResolveUrl("~/mypage.aspx/getResults") %>',
dataType: 'json',
type: "POST",
params: {
contentType: 'application/json; charset=utf-8'
},
quietMillis: 100,
data: function (term, page) {
return JSON.stringify({ q: term, page_limit: 10 });
},
results: function (data) {
console.log(data);
return { results: data };
}
}
For more information, you can read
http://stackoverflow.com/questions/20846024/select2-with-ajax-asp-net-4-0-webmethod-request[
^]