ajax调用不会触发服务器端Web方法

时间:2017-07-06 06:17:55

标签: jquery asp.net ajax

我有一个asp.net网页,我在尝试通过ajax调用输入用户名时验证用户名,但调用返回未定义的值,当我尝试调试它时,我发现控件没有到达服务器端Web方法码。 ajax调用代码是

$(function () {

    $("#ContentPlaceHolder2_txtUserName").change(checkUser);
});

function checkUser() {
    var usernm = $('#ContentPlaceHolder2_txtUserName').val();
    $.ajax({
        type: "POST",
        url: "AdminHome.aspx/CheckUser",
        data: { username: usernm },
        success: function (data) {
            //var data = $.parseJSON(data.d);
            alert(data);
            $("#ContentPlaceHolder2_lblError").empty();
            if (data.d != "0") {
                $("#ContentPlaceHolder2_lblError").html(' That user name has already been taken');
            }
        }
    });

}

服务器端代码如下

[WebMethod]
    public static int CheckUser(string username)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnect"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("SP_CheckUserName", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@username", username);
            conn.Open();
            int result= (int)cmd.ExecuteScalar();
            return result;
        }
    }

0 个答案:

没有答案