0

我刚刚在我的项目中尝试了 fcbkcomplete,但是我在调​​用 ajax 请求以将结果获取到自动完成时遇到了麻烦,有人知道如何完成吗?

服务器端 :

  public static string tagFriendAutocomplete()
{
    string Result = "";
    string query = "select fr.FRIEND_ID,c.[USER_NAME] from clients c inner join friends fr on c.CLIENT_ID=fr.FRIEND_ID and fr.CLIENT_ID=1 and c.[USER_NAME] like '%a%' ";

    DataTable dt = new SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).getQueryResult(query);
    if (dt.Rows.Count > 0)
    {
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            Result += dt.Rows[i]["USER_NAME"] + ",";
        }
    }

    return Result;
}

客户端 :

$(document).ready(function () {
            $("#select3").fcbkcomplete({
                //"autoCompleteFriendTag/data.txt",
                addontab: true,
                maxitems: 10,
                input_min_size: 0,
                height: 10,
                cache: true,
                newel: false,
                select_all_text: "select"
            });

 });

我不知道在哪里放置 jquery ajax 代码任何帮助请我已经 2 天寻找解决方案,但遗憾的是我找不到我的 ajax 代码是:

$.ajax({
                type: 'POST',
                url: 'Testautocomplete.aspx/tagFriendAutocomplete',
                data: "{}",
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (data) {

                  return  data.d.split(',');

                },
                error: function (xhr) {
                    alert("responseText: " + xhr.responseText);
                }
            });
4

1 回答 1

0

尝试这个;

$(document).ready(function () {
        $("#select3").fcbkcomplete({
            json_url: 'Testautocomplete.aspx/tagFriendAutocomplete',
            addontab: true,
            maxitems: 10,
            input_min_size: 0,
            height: 10,
            cache: true,
            newel: false,
            select_all_text: "select"
        });
});

有一点很重要,您必须json通过序列化数据以格式发送数据。

你后面的代码应该是这样的;

public static string tagFriendAutocomplete()
{

   Dictionary<> jsonResult= new Dictionary<>();  
   string query = "select fr.FRIEND_ID,c.[USER_NAME] from clients c inner join friends fr           on c.CLIENT_ID=fr.FRIEND_ID and fr.CLIENT_ID=1 and c.[USER_NAME] like '%a%' ";

   DataTable dt = new  SQLHelper(SQLHelper.ConnectionStrings.WebSiteConnectionString).getQueryResult(query);
   if (dt.Rows.Count > 0)
   {
     for (int i = 0; i < dt.Rows.Count; i++)
      {
        jsonResult.Add(dt.Rows[i]["USER_NAME"], i); 
      }
   }

   var result = from r in jsonResult select new  
    {  
       key = r.Key,  
       value = r.Value  
    };  

   JavaScriptSerializer ser = new JavaScriptSerializer();
   context.Response.Write(ser.Serialize(result));
}
于 2013-04-09T13:59:44.590 回答