2014-10-11 47 views
1

当我使用下面的代码从Web服务中获取它时,我无法看到catcomplete自动完成智能感知。我的web服务返回以下字符串Jquery Autocomplete()/ Catcomplete()函数不工作

[{label:"TechCrunch",category:"Website"},{label:"Techcrunch",category:"Tag"},  {label:"techno",category:"Tag"}] 


$(document).ready(function() { 
     $("#<%= txtinputtag.ClientID%>").catcomplete({ 
     source: function(request,response){//error if I hard code this ajax call will with a an array works fine 
       $.ajax({ 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        url: "../Tag/Follow.aspx/GetIntellisense", 
        dataType: "json", 
        data: "{'searchtext':'" + request.term + "'}", 
        success: function (data) { 
         if (data.d != "") { 
          //console.log(data.d);//this show the desired json output as returned from the web service 
          response(data.d); 
         } 
        } 
       }); 
      }, 
      select: function(event, ui) { 
       $(this).val(""); 
       return false; 
      }, 
     }); 
    }); 

这是返回一个字符串的WebMethod

[WebMethod] 
     public static string GetIntellisense(string searchtext) 
    { 
     Debugger.Launch(); 
     var uc = new UtilityClass(); 

     List<DTOWebsite> lstDtoWebsites = uc.GetIntellisense(searchtext); 

     string str = "["; 

     foreach (DTOWebsite dto in lstDtoWebsites) 
     { 
      str += "{label:\""+dto.WebSiteName +"\",category:\""+dto.WebsiteType +"\"},"; 

     } 
     str = str.Remove(str.Length-1,1); 
     str += "]"; 

     return str.ToString(); 
    } 

回答

0

你需要接受在分配给source参数的函数2个参数:

  1. request已搜到的term

  2. response是你结果传递给

    source: function(request, response){ ... 
    

http://jsfiddle.net/h7uahuhx/

+0

上述更改后返回我这个回调函数。 Uncaught TypeError:无法使用'in'运算符在[{label:“TechCrunch”,category:“Website”},{label:“Techcrunch”,category:“Tag”},{label:“技术“,类别:”标签“}] 我也更新了这个问题 – 2014-10-11 18:58:45

+0

@KinnanNawaz它看起来不像这个错误来自你的问题中的代码。你脚本中的'in'运算符在哪里? – Dmitry 2014-10-11 19:16:51

+0

我没有声明一个“in”运算符,它来自Jquery,即源“jquery-1.10.2.js:985”line 985 – 2014-10-11 19:24:42