I am trying to implement AJAX autocomplete in ASP.Net using jQuery. This is my table definition:
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
)
My Webpage i.e. 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>
And This is the Webmethod I created in Autocomplete.aspx.cs
[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;
}
Now this produces an alert which says 'unknown'. Please help.
alert()you see is from yourerrorhandler. Check the console for more information on exactly what the error is. Also, are you really using jQuery 1.4.2? That's massively out of date. I'd strongly suggest you upgrade to at least 1.12.4