jQuery自动完成在托管网站上不起作用

本文关键字:网站 不起作用 jQuery | 更新日期: 2023-09-27 18:29:41

我正在使用以下代码自动完成网站中的搜索文本框。代码在本地主机上运行良好,但当站点托管时,会弹出"错误"框。

脚本:

$(document).ready(function () {
    SearchText();
});
function SearchText() {
    $(".autosuggest").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Index.aspx/GetAutoCompleteData",
                data: "{'location':'" + document.getElementById('ContentPlaceHolder1_txtSearch').value + "'}",
                dataType: "json",
                success: function (data) {
                    response(data.d);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }
    });
}

功能代码为:

[WebMethod]
public static List<string> GetAutoCompleteData(string location)
{
    List<string> result = new List<string>();
    string connect = @"
         Data Source=jaipurrooms.db.11458954.hostedresource.com; Initial Catalog=jaipurrooms;
         User ID=xyz;
         Password=xyz;";
    using (SqlConnection con = new SqlConnection(connect))
    {
        using (SqlCommand cmd = new SqlCommand("select DISTINCT Location from Details where Location LIKE '%'+@location+'%'", con))
        {
            con.Open();
            cmd.Parameters.AddWithValue("@location", location);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                result.Add(dr["Location"].ToString());
            }
            return result;
        }
    }
}

jQuery自动完成在托管网站上不起作用

我认为您必须在ajax请求中检查您的url。尝试使用以下语法:

url: "./Index.aspx/GetAutoCompleteData",