jQuery自动完成,调用函数的问题

时间:2013-12-17 10:22:47

标签: c# javascript jquery asp.net webforms

我在屏幕上运行jQuery自动完成,该屏幕调用该函数从另一个.aspx页面获取数据。设置在本地运行时工作正常,但是当我托管它时,我发生错误,这是我设置的消息,当它不起作用时。唯一的问题是我无法弄清楚为什么它会在本地工作但不能在托管时(顺便提一下在localhost上)。

带有自动填充框的ASPX页面:

$(document).ready(function () {
    document.getElementById('<%=txtCustomerType.ClientID %>').onclick = SearchText();
});

    function SearchText() {
        $(".autosuggest").autocomplete({
            source: function (request, response) {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "/../JQueryAutoComplete.aspx/GetAutoCompleteData",
                    data: "{'Customer':'" + document.getElementById(
                        '<%=txtCustomerType.ClientID %>').value + "'}",
                dataType: "json",
                success: function (data) {
                    response(data.d);
                },
                error: function (result) {
                    alert('An error occurred!');
                }
            });
        }
    });

    $(".autosuggest").autocomplete({
        select: function (a, b) {
            checkDirty = false;
            $(this).val(b.item.value);
            $("form").submit()
        }
    });

它调用的Jquery页面后面的代码:

public partial class _Default : System.Web.UI.Page {

    protected void Page_Load(object sender, EventArgs e) {

    }
    [WebMethod]


    public static List<string> GetAutoCompleteData(string Customer) {
        List<string> result = new List<string>();

        SqlConnection dbConnection = new SqlConnection(GetConnection.GetConnectionString());

        using (SqlConnection con = dbConnection) {
            DataSet ds = new DataSet();
            using (SqlCommand cmd = new SqlCommand(
                    "PL_CustomerTypes_IntelliSearch", dbConnection)) {

                cmd.Parameters.Add("@SearchText", SqlDbType.NVarChar).Value = Customer;
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;

                da.Fill(ds);

                DataTable dtResults = new DataTable();
                dtResults = ds.Tables[0];

                foreach (DataRow R in dtResults.Rows) {
                    string strResult = (string)R.ItemArray[0];
                    result.Add(strResult);
                }


                //while (dr.Read()) {
                //    string Name = (dr["ACNT_NAME"].ToString().Trim());
                //    result.Add(Name);
                //}

                return result;

            }
        }
    }

}

2 个答案:

答案 0 :(得分:1)

检查网址:/../JQueryAutoComplete.aspx/GetAutoCompleteData。重写它而不诉诸“../".

另外,为了确保您可以从错误函数中获取更多信息(请参阅http://api.jquery.com/jQuery.ajax/),其签名是 函数(jQxhr,textStatus,errorThrown)[而不是函数(结果)],使用它作为错误处理程序,以便检查它是否因404或其他原因而失败:

function(xhr){
  alert(xhr.status + " - " + xhr.statusText);
}

xhr.status将打印http状态代码,例如404,500等 xhr.statusText将打印与代码关联的文本,例如Page not Found,Internal Server Error等

答案 1 :(得分:0)

问题在于JQueryAutoComplete.aspx中的声明需要将属性代码文件更改为代码隐藏。

codefile属性阻止它访问.cs以及404来自的位置。

相关问题