2012-05-18 124 views
1

這是讓我瘋狂,我只是不斷收到消息「錯誤」沒有別的。我有這個自動完成工作與AJAX工具包,但我想嘗試JQuery,我對JQuery有很少的經驗。下面是WebService的代碼:在ASP.Net JQUery自動完成Web服務

[WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService] 
public class WebService : System.Web.Services.WebService { 

public WebService() { 

    //Uncomment the following line if using designed components 
    //InitializeComponent(); 
} 

[WebMethod] 
public static string GetNames(string prefixText, int count) 
{ 
    Trie ArtistTrie = new Trie(); 
    if (HttpContext.Current.Cache["CustomersTrie"] == null) 
    {   
     SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString); 
     SqlCommand comm = new SqlCommand(); 
     comm.CommandText = "SELECT * FROM TopArtists ORDER BY name ASC"; 
     SqlDataAdapter da = new SqlDataAdapter(); 
     DataTable dt = new DataTable();    
     da.SelectCommand = comm; 
     comm.Connection = conn; 
     conn.Open(); 
     da.Fill(dt); 
     conn.Close(); 
     Trie newtrie = new Trie(); 
     foreach (DataRow dr in dt.Rows) 
     { 
      // topartists.Add(dr[0].ToString()); 
      newtrie.Add(dr[0].ToString()); 
     } 
     HttpContext.Current.Cache["CustomersTrie"] = newtrie; 
    } 
    ArtistTrie = (Trie)HttpContext.Current.Cache["CustomersTrie"]; 

    List<string> list = ArtistTrie.GetCompletionList(prefixText, 10); 
    List<Band> list1 = new List<Band>(); 
    foreach (string a in list) 
    { 
     Band newband = new Band(); 
     newband.Name = a; 
     list1.Add(newband); 
    } 
    string json = JsonConvert.SerializeObject(list1, Formatting.Indented); 
    return json; 

} 

這裏是jQuery代碼:

<script type="text/javascript"> 
     $(document).ready(function() { 
     $(function() { 
      $("#tb1").autocomplete({ 

     source: function (request, response) { 
      $.ajax({ 
       url: "WebService.asmx/GetNames", 
       data: request.term , 
       dataType: "json", 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 

       error: function (XMLHttpRequest, textStatus, errorThrown) { 
        alert(textStatus); 
       } 
      }); 
     }, 
     minLength: 2 
     }); 
     }); 
     }) 
    </script> 

回答

2

好了,您的jQuery代碼有錯誤,包括在最後一個缺少分號和不必要的功能包裝的自動完成,嘗試:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#tb1").autocomplete({ 
      source: function(request, response) { 
       $.ajax({ 
        url: "WebService.asmx/GetNames", 
        data: request.term, 
        dataType: "json", 
        type: "POST", 
        contentType: "application/json; charset=utf-8", 
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
         alert(textStatus); 
        } 
       }); 
      }, 
      minLength: 2 
     }); 
    }); 
</script> 
1

爲Ajax部分,返回需要在陣列中,串[]

示例代碼,

[WebMethod] 
    public string[] area(string prefixText) 
    { 
     List<string> listString = new List<string>(); 
     using (SqlConnection con = new SqlConnection("Initial Catalog=EMS;Server=S-CEMSDB01;User ID=sa;Password=sqltest")) 
     { 

      SqlCommand cm = new SqlCommand("select distinct eqp_location from EQUIPMENT where eqp_location like '" + prefixText + "%' order by eqp_location", con); 
      con.Open(); 
      SqlDataReader dr = cm.ExecuteReader(); 
      if (dr.HasRows) 
      { 
       while (dr.Read()) 
       { 
        listString.Add((dr["eqp_location"].ToString())); 

        //c.FullName, serializer.Serialize(c)) 
       } 
      } 
      dr.Close(); 
      dr.Dispose(); 
      con.Close(); 
     } 
     string[] str = listString.ToArray(); 
     return str; 

    }