【问题标题】:AJAX autocomplete with jQuery in ASP.Net在 ASP.Net 中使用 jQuery 进行 AJAX 自动完成
【发布时间】: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


【解决方案1】:

您在 aspx 中的 [WebMethod] 必须是 static 才能工作!

url: '<%=ResolveUrl("Autocomplete.aspx/binddata") %>'

[WebMethod]
public static List<string> binddata(string name)
{
    //your code
}

【讨论】:

    猜你喜欢
    • 2013-02-20
    • 2012-01-14
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多