开发者

JQuery Autocomplete with ASp.Net webservice

开发者 https://www.devze.com 2023-04-12 17:06 出处:网络
I am working on the JQuery Autocomplete using ASP.Net webservice. I have ASP.Net webservice being called in JQuery (JSON) drop down as

I am working on the JQuery Autocomplete using ASP.Net webservice. I have ASP.Net webservice being called in JQuery (JSON) drop down as

 $(document).ready(function () {
    $("#txtTest").autocomplete({ 
         source: function (request, response) {  
             $.ajax({  
                 type: "POST",  
                 contentType: "application/json; charset=utf-8", 
                 url: "Webservice.asmx/GetNames",
                 data: "{'prefix':'" + request.term + "'}",  
                 dataType: "json",  
                 async: true,  
                 success: function (data){  
                    response($.map(data, function(item)
                    { return item ; }));  
                },  
                error: function (result) {  
                   alert("Due to unexpected errors we were unable to load data");  
                }  
             });
         },  
         minLength:2
     });
 });

And i am getting the output on the drop of auto-complete as

 {"First":"Steve","Second":"AK"}
 {"First":"Evet","Second":"EV"}
 {"First":"Stevens","Second":"SV"}

How do i display the "First" items alone (Like Steve, Evet, 开发者_如何学CStevens) as the output of the drop down auto-complete?

Please help me!


You need to look at the formatItem option on the AutoComplete method - try this

formatItem: function(row, i, n) {
      return row.First;
  }


this would probably do the trick

   success: function (data)
    {  
        response($.map(data, function (item)
         {
             return { label: item.First, value: item.First}
         }))
    });


Take a look at my answer to this one: Jquery Autocomplete 2 Fields

but use your values instead of "A" and "B" in the fields.

In addition, you (might) need a converter to handle the asp.net data:

$.ajax({
    url: "Webservice.asmx/GetNames",
    dataType: "jsond",
    type: "POST",
    contentType: "application/json",
    converters: {
        "json jsond": function(msg)
        {
            return msg.hasOwnProperty('d') ? msg.d : msg;
        }
    },
...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号