ajax没有打电话

时间:2017-06-27 13:35:57

标签: javascript c# ajax

我试图调用ajax然后ajax将调用'WebMethod`,但我的AJAX没有被执行。

以下是TextBox.TextChanged事件

的HTML代码
<input id="usernamesignup" name="usernamesignup" required="required" type="text"  placeholder="User Name" runat ="server" onchange="checkUserName();"/>

对于AJAX,我编写了JavaScript函数

function checkUserName()
    {
        debugger;
        $.ajax({

            type: "POST",
            async: true,
            url: "SignUp1.aspx/CheckUserNameAvailability",
            data: '{username: "'+ $("#<%=usernamesignup.ClientID%>").value.trim() +'" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
              var msg = $("#<%=LabelStatus.ClientID%>")[0];
                switch (response.d) {
                    case "true":
                        msg.style.display = "block";
                        msg.style.color = "red";
                        msg.innerHTML = "User Name Or Email already exists.";
                        break;
                    case "false":
                        msg.style.display = "block";
                        msg.style.color = "green";
                        msg.innerHTML = "User Name Or Email Available";
                        break;
                }

            }
        });

    }

我的WebMethod

    [System.Web.Services.WebMethod]
    public static int CheckUserNameAvailability(string username)
    {
        string conString = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
        using (MySqlConnection conn = new MySqlConnection(conString))
        {
            using (MySqlCommand cmd = new MySqlCommand("spCheckUserNameAvailability", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("UserName", username);
                conn.Open();
                return (int)cmd.ExecuteScalar();
            }
        }
    }

请有人建议我对此代码进行一些更改以使其正常工作

2 个答案:

答案 0 :(得分:0)

你确定你的方法没有被调用吗?你添加了一个断点吗?

在您的Javascript中,您正在检查“true”或“false”;

 switch (response.d) {
                    case "true":

但是,在服务器端代码中,您将返回一个Integer;

return (int)cmd.ExecuteScalar();

因此,您的交换机永远不会遇到任何情况。

考虑在浏览器中打开Developer控制台(F12)并在“控制台”选项卡中查看Javascript错误,然后在“网络”选项卡中查看AJAX调用的结果,以便正确排除故障。

答案 1 :(得分:0)

听起来像渲染你的HTML时,它不知道那个函数是什么。

尝试将听众放入你的js。

(function() {
//self invoking function to wait for dom.

function checkUserName()
    {
        debugger;
        $.ajax({

            type: "POST",
            async: true,
            url: "SignUp1.aspx/CheckUserNameAvailability",
            data: '{username: "'+ $("#<%=usernamesignup.ClientID%>").value.trim() +'" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
              var msg = $("#<%=LabelStatus.ClientID%>")[0];
                switch (response.d) {
                    case "true":
                        msg.style.display = "block";
                        msg.style.color = "red";
                        msg.innerHTML = "User Name Or Email already exists.";
                        break;
                    case "false":
                        msg.style.display = "block";
                        msg.style.color = "green";
                        msg.innerHTML = "User Name Or Email Available";
                        break;
                }

            }
        });

    }
    
    var myInput = document.getElementById("usernamesignup");
    myInput.addEventListener("change", checkUserName);
    
})