当WebService返回正确的数据时,jQuery自动完成显示未定义的值

时间:2012-11-03 09:43:48

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

$(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();       
}

1 个答案:

答案 0 :(得分:0)

尝试更改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