【发布时间】:2016-12-12 09:35:59
【问题描述】:
我正在尝试使用 jQuery 在 ASP.Net 中实现 AJAX 自动完成功能。这是我的表定义:
CREATE TABLE [dbo].[tbluser](
[nid] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](100) NULL,
[email] [varchar](100) NULL,
[address] [varchar](200) NULL,
[password] [varchar](100) NULL
)
我的网页,即 Autocomplete.aspx
<head runat="server">
<title></title>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/themes/base/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"></script>
<script>
$(function () {
$("#superheroz").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: '<%=ResolveUrl("Autocomplete.aspx/binddata") %>',
dataType: "json",
data: "{ 'name': '" + request.term + "'}",
success: function (data) {
response(data.d);
},
error: function (result) {
alert(response.responseText);
},
failure: function () {
response.responseText
}
});
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="text" id="superheroz" runat="server" />
</div>
</form>
</body>
这是我在 Autocomplete.aspx.cs 中创建的 Webmethod
[WebMethod]
public List<string> binddata(string name)
{
List<string> ud = new List<string>();
string sql = "select * from tbluser where name like '%" + name + "%'";
ds = new DataSet();
ds = da.getData(sql);
if (ds.Tables[0].Rows.Count != 0)
{
foreach (DataRow dr in ds.Tables[0].Rows)
{
string su;
su = dr["name"].ToString();
ud.Add(su);
}
}
return ud;
}
现在这会产生一个提示“未知”的警报。请帮忙。
【问题讨论】:
-
您看到的
alert()来自您的error处理程序。检查控制台以获取有关错误的详细信息。另外,你真的在使用 jQuery 1.4.2 吗?那已经过时了。我强烈建议您至少升级到 1.12.4
标签: c# jquery asp.net ajax jquery-ui-autocomplete