【问题标题】:jQuery autocomplete shows undefined values while WebService is returning correct datajQuery 自动完成显示未定义的值,而 WebService 正在返回正确的数据
【发布时间】:2012-11-03 09:43:48
【问题描述】:

$(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 方法。谁能帮我?。 GetCompletionList WebService 方法返回字符串列表,但 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();       
}

【问题讨论】:

  • 从哪里获取要在自动完成中显示的值

标签: c# asp.net ajax web-services


【解决方案1】:

尝试改变ajax调用中的数据如下

如果为 asp 控件完成了自动完成

 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 +@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;
            }
        }
    }

}

.. 希望这会有所帮助:D

【讨论】:

  • 那么如果从日期库中获取值,那么您使用的 Web 服务很可能是错误的,请参阅我的答案已更改
  • 如果上述答案不清楚,您可以提出任何问题
  • 实际上,OP 唯一必须改变的就是成功处理程序并按照您的建议使用 response(data.d)
猜你喜欢
  • 1970-01-01
  • 2023-03-18
  • 2016-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-01
  • 1970-01-01
相关资源
最近更新 更多