C# 当WebService返回正确数据时,jQuery自动完成显示未定义的值
})); 这是我的jQuery代码和C# 当WebService返回正确数据时,jQuery自动完成显示未定义的值,c#,asp.net,ajax,web-services,C#,Asp.net,Ajax,Web Services,})); 这是我的jQuery代码和WebService方法。有人能帮我吗GetCompletionListWebService方法返回字符串列表,但文本框上的autocomplete显示所有值的未定义 $(function() { $(".tb").autocomplete({ source: function(request, response) { $.ajax({ url: "MyService.asmx/GetCompletionList",
WebService
方法。有人能帮我吗GetCompletionList
WebService
方法返回字符串列表,但文本框上的autocomplete
显示所有值的未定义
$(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
});
public List GetCompletionList(字符串前缀)
{
RegistrationBAL\u rbal=新注册bal(SessionContext.SystemUser);
数据集ds=新数据集();
_rbal.LoadByContextSearch(ds,prefixText);
List myList=新列表();
foreach(ds.Tables[0].行中的DataRow行)
{
myList.Add((字符串)行[0]);
}
返回myList.ToList();
}
尝试如下更改ajax调用中的数据
data: "{'prefixText':'" + document.getElementById("<%= ContactName.ClientID %>").value + "'}",
如果asp控件已完成自动完成
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();
}
编辑自动完成功能的答案
data: "{'prefixText':'" + document.getElementById("requiredID").value + "'}",
函数SearchText(){
$(“.autosuggest”).autocomplete({
来源:功能(请求、响应){
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“AutoCompleteService.asmx/GetAutoCompleteData”,
数据:“{'PhoneContactName':'”+document.getElementById(“”.value+“}”,
数据类型:“json”,
成功:功能(数据){
答复(数据d);
},
错误:函数(结果){
//警报(“错误”);
}
});
}
});
}
这类似于ajax功能,但在服务器端,我使用了web服务,如下所示,web服务从数据库中获取值
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");
}
});
}
});
}
公共类自动完成服务:System.Web.Services.WebService
{
[网络方法]
公共列表GetAutoCompleteData(字符串PhoneContactName)
{
列表结果=新列表();
串查询串;
QueryString=System.Configuration.ConfigurationManager.ConnectionString[“Admin_RaghuConnectionString”].ToString();
使用(SqlConnection obj_SqlConnection=newsqlconnection(QueryString))
{
使用(SqlCommand obj_SqlCommand=new SqlCommand(“从PhoneContactName中选择不同的PhoneContactName,其中PhoneContactName类似+@SearchText+'%”,obj_SqlConnection))
{
obj_SqlConnection.Open();
obj_Sqlcommand.Parameters.AddWithValue(“@SearchText”,PhoneContactName);
SqlDataReader obj_result=obj_Sqlcommand.ExecuteReader();
while(obj_result.Read())
{
添加(obj_result[“PhoneContactName”].ToString().TrimEnd());
}
返回结果;
}
}
}
}
。。希望这对您有所帮助:D从哪里获取要在自动完成中显示的值如果从数据库获取值,则您使用的web服务很可能是错误的。请查看我的答案,该答案已更改。如果上述答案不清楚,您可以问任何类型的问题实际上,OP必须更改的唯一一件事是成功处理程序,并按照您的建议使用响应(data.d)
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 +@SearchText+'%'", 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;
}
}
}
}