JQuery未定义错误自动完成

时间:2015-12-30 02:18:30

标签: jquery

我有以下脚本的JQuery错误。当我运行脚本时会弹出一个警告,并显示“Undefined”。我做了搜索,发现常见原因是脚本顺序,CDN已关闭。 CDN不是因为我在另一页上引用了脚本而且工作正常。我做了一个警报作为测试,我的Javascript工作正常。第三,我把脚本和页面底部放在一起,我仍然遇到同样的错误。第四,我与页面上的另一个脚本没有冲突,因为它是唯一的脚本。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <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 type="text/javascript">  
        $(function () {
            $("#txtCity").autocomplete({
                source: function (request, response) {
                    var param = { cityname: $('#txtCity').val() };
                    $.ajax({
                        url: "testAutoComplete.aspx/GetCities",
                        data: JSON.stringify(param),
                        dataType: "json",
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataFilter: function (data) { return data; },
                        success: function (data) {
                            response($.map(data.d, function (item) {
                                return {
                                    value: item
                                }
                            }))
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(errorThrown);
                        }
                    });
                },
                minLength: 2 
            });
        });         
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="label1" Text="Enter City: " runat="server" />
    <asp:TextBox ID="txtCity" runat="server" />
    </div>
    </form>
</body>
</html>

[WebMethod]
public static List<string> GetCities(string cityname)
{
    List<string> City = new List<string>();
    SqlConnection conn;
    SqlCommand comm;
    SqlDataReader reader;
    string connectionString = ConfigurationManager.ConnectionStrings["SlotConn"].ConnectionString;
    conn = new SqlConnection(connectionString);
    comm = new SqlCommand("SELECT Cityname FROM City WHERE Cityname LIKE @Cityname", conn);
    comm.Parameters.Add("@Cityname", System.Data.SqlDbType.varchar);
    comm.Parameters["@Cityname"].Value = cityname;
    try
    {
        conn.Open();
        reader = comm.ExecuteReader();
        while (reader.Read())
        {
            City.Add(reader.GetString(0));
        }
    }

    finally
    {
        conn.Close();
    }
    return City;
}   

0 个答案:

没有答案