2014-10-11 108 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