Jquery autocomplete .NET WebMethod

asp.net, c#, jquery

Solution

Both implementations of HttpHandler and Web-Services perform identically, however I prefer HttpHandler as it is lightweight, on the other side a web-service encodes request & response xml data which adds extra payload.

POP JqueryUI Autocomplete with web-methods: http://blog.nitinsawant.com/2011/09/integrating-jquery-ui-autocomplete-in.html

JS:

$(document).ready(function () {
            $("#<%=txtAutoComplete.ClientID %>").autocomplete({
                source: function (request, response) {
                    $.ajax({
                        url: "webservice/TestService.asmx/SearchData",
                        data: "{ 'q': '" + request.term + "', 'limit': '10' }",
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    label: item.Name,
                                    value: item.id + ""
                                }
                            }))
                        }
                    });
                }
            });
});

C#:

[System.Web.Services.WebMethodAttribute(),  System.Web.Script.Services.ScriptMethodAttribute()]
public List<tdata> SearchData(string q, int limit)
{
    return new List<tdata> { new tdata { id = 0, name = "nitin" } };
}

Problem

I have Jquery autocomplete working with a HttpHandler - .ashx file. It works fine, I am wondering is there an easy way to use the autocomplete with a [WebMethod] right in the code behind - and are there any advantages to this?

Original source