2012-11-03 71 views
0

jQuery的自動完成顯示而web服務將返回正確的數據

$(function() { 
$(".tb").autocomplete({ 
    source: function(request, response) { 
     $.ajax({ 
     url: "MyService.asmx/GetCompletionList", 
     data: "{ 'prefixText': '" + request.term + "' }", 
      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 { 
         value: item.Email 
        } 
       })) 
      }, 
      error: function(XMLHttpRequest, textStatus, errorThrown) { 
       alert(textStatus); 
      } 
     }); 
    }, 
    minLength: 1 
}); 

})未定義的值;

這是我的jQuery代碼和WebService方法。誰能幫我?。 GetCompletionListWebService方法如果自動完成是一個ASP的控制做了所有的值返回字符串,但自動完成對TextBox節目undefined列表

public List<string> GetCompletionList(string prefixText) 
{ 
    RegistrationBAL _rbal = new RegistrationBAL(SessionContext.SystemUser); 
    DataSet ds = new DataSet(); 
    _rbal.LoadByContextSearch(ds, prefixText); 

    List<string> myList = new List<string>(); 
    foreach (DataRow row in ds.Tables[0].Rows) 
    { 
     myList.Add((string)row[0]); 
    } 
    return myList.ToList();  
} 
+0

您從哪裏獲取要顯示的自動完成值 – Raghurocks

回答

0

試圖改變在Ajax調用的數據如下

data: "{'prefixText':'" + document.getElementById("<%= ContactName.ClientID %>").value + "'}", 

否則給予如下

data: "{'prefixText':'" + document.getElementById("requiredID").value + "'}", 

編輯答案在自動完成工程

function SearchText() { 
    $(".autosuggest").autocomplete({ 
     source: function (request, response) { 
      $.ajax({ 
       type: "POST", 
       contentType: "application/json; charset=utf-8", 
       url: "AutoCompleteService.asmx/GetAutoCompleteData", 
       data: "{'PhoneContactName':'" + document.getElementById("<%= ContactName.ClientID %>").value + "'}", 
       dataType: "json", 
       success: function (data) { 
        response(data.d); 
       }, 
       error: function (result) { 
        //alert("Error"); 
       } 
      }); 
     } 
    }); 
} 

這類似於你的Ajax功能,但在服務器端我用Web服務爲低於從數據庫中獲取值

public class AutoCompleteService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public List<string> GetAutoCompleteData(string PhoneContactName) 
    { 
     List<string> result = new List<string>(); 
     string QueryString; 
     QueryString = System.Configuration.ConfigurationManager.ConnectionStrings["Admin_raghuConnectionString1"].ToString(); 

     using (SqlConnection obj_SqlConnection = new SqlConnection(QueryString)) 
     { 
      using (SqlCommand obj_Sqlcommand = new SqlCommand("select DISTINCT PhoneContactName from PhoneContacts where PhoneContactName LIKE [email protected]+'%'", obj_SqlConnection)) 
      { 
       obj_SqlConnection.Open(); 
       obj_Sqlcommand.Parameters.AddWithValue("@SearchText", PhoneContactName); 
       SqlDataReader obj_result = obj_Sqlcommand.ExecuteReader(); 
       while (obj_result.Read()) 
       { 
        result.Add(obj_result["PhoneContactName"].ToString().TrimEnd()); 
       } 
       return result; 
      } 
     } 
    } 

} 

。希望這有助於:D

+0

無法正常工作............. –

+0

那麼最有可能的是您使用的Web服務是錯誤的如果ar e從數據庫中獲取值看到我的答案是變化的 – Raghurocks

+0

如果上述答案不明確,您可以詢問任何類型的查詢 – Raghurocks

相關問題