0

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

1 回答 1

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

于 2012-11-03T09:56:43.600 回答