我刚刚在我的项目中尝试了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);
}
});
答案 0 :(得分: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));
}