You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

jQuery Autocomplete选择事件绑定C# WebMethod失效问题排查

问题分析与解决方案

核心问题定位

你遇到的response未定义错误,根源在于错误地在普通AJAX的success回调中使用了jQuery Autocomplete专属的response函数:

  • response是Autocomplete组件source回调的内置参数,仅用于向组件返回自动补全选项列表
  • 在GetCustomerDetails的AJAX success回调上下文里,这个函数根本不存在,导致代码执行中断,无法完成表格渲染逻辑

修正后的代码

只需要移除response(...)的包裹,直接执行$.map遍历数据并生成表格行即可:

function GetCustomerDetails(country) {
    $("#tblCustomers tbody tr").remove();
    $.ajax({
        type: "POST",
        url: "Default4.aspx/GetCustomers",
        data: '{country: "' + country + '" }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            // 移除多余的response()包裹,直接遍历数据渲染
            $.map(data.d, function (item) {
                var rows = "<tr>" + 
                    "<td class='customertd'>" + item.Name + "</td>" + 
                    "<td class='customertd'>" + item.Population + "</td>" + 
                    "<td class='customertd'>" + item.Code + "</td>" + 
                    "<td class='customertd'>" + item.Continent + "</td>" + 
                    "<td class='customertd'>" + item.Region + "</td>" + 
                    "<td class='customertd'>" + item.GovernmentForm + "</td>" + 
                "</tr>";
                $('#tblCustomers tbody').append(rows);
            });
        },
        // 修正错误回调的参数,原failure改为标准的error,参数更准确
        error: function (xhr, status, error) {
            alert("获取客户详情失败: " + error);
        }
    });
}

额外优化建议

  1. 防止SQL注入:你的C#代码中用string.Format拼接SQL语句存在注入风险,建议改用参数化查询:
[WebMethod]
public static Customer[] GetCustomers(string country) {
    List<Customer> customers = new List<Customer>();
    string CS = ConfigurationManager.ConnectionStrings["cn3"].ConnectionString;
    // 使用参数化查询,避免注入风险
    string query = "SELECT `Name`, `Population`, `Code`, `Continent`, `Region`, `GovernmentForm` FROM `country` WHERE `Name` LIKE @Country;";
    using (MySqlConnection con = new MySqlConnection(CS)) {
        using (MySqlCommand cmd = new MySqlCommand(query, con)) {
            // 添加参数,自动处理字符串转义
            cmd.Parameters.AddWithValue("@Country", "%" + country + "%");
            con.Open();
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read()) {
                Customer customer = new Customer();
                customer.Name = reader.GetString(0);
                customer.Population = reader.GetString(1);
                customer.Code = reader.GetString(2);
                customer.Continent = reader.GetString(3);
                customer.Region = reader.GetString(4);
                customer.GovernmentForm = reader.GetString(5);
                customers.Add(customer);
            }
        }
    }
    return customers.ToArray();
}
  1. 升级jQuery版本:你使用的jQuery 1.5和jQuery UI 1.8是非常老旧的版本,存在安全漏洞和兼容性问题,建议升级到较新的稳定版本。

内容的提问来源于stack exchange,提问作者Iter Lsic Iealf

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 15:27:39