使用Javascript / Json自动完成只会返回错误框

时间:2014-10-15 14:16:12

标签: javascript c# jquery asp.net json

我正在使用我认为可能导致问题的用户控件,因为我所遵循的示例是引用实际的aspx默认页面,但有人可能会帮助我。我正在尝试设置一个自动完成文本框,所有我似乎都得到错误弹出窗口。无法弄清楚我的问题在哪里。我已粘贴整个代码,但Page_Load程序除外。


HTML

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Search Textbox.ascx.cs" Inherits="StagingApplication.Search.Search_Textbox" %>
<link href="../css/customPages.css" rel="stylesheet" />
<link href="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/themes/smoothness/jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
    $("#txtAutoComplete").autocomplete({
        source: function (request, response) {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "Search/Page%20Search%20Textbox.aspx/GetCategory",
                data: "{'CountryName':'" + $("#txtAutoComplete").val() + "'}",
                dataType: "json",
                success: function (data) {
                    response(data.d);
                },
                error: function (result) {
                    alert("Error");
                }
            });
        }
    });
});
</script>
<div>
    <asp:TextBox ID="searchText" runat="server" class="textboxLargeInline" AutoCompleteType="Search"></asp:TextBox>
    <asp:Button ID="searchButton" runat="server" class="standardButton" OnClick="searchButton_Click"/>
    <div>
        <input type="text" id="txtAutoComplete" />
    </div>
</div>

代码背后

[WebMethod]
public static string[] GetCategory(string term)
{
    List<string> retCategory = new List<string>();
    string ConnectionString = Connect.Connect("Location");
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        string query = string.Format("SELECT TOP 1000 [CountryName] FROM [Location].[dbo].[Country] WHERE [CountryName] LIKE '%{0}%'", term);
        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                retCategory.Add(reader.GetString(0));
            }
        }
        con.Close();
    }
    return retCategory.ToArray();
}

0 个答案:

没有答案