Ajax函数总是返回false

时间:2017-01-20 06:46:36

标签: javascript ajax web-services asp.net-mvc-5

我有web服务检查数据库中是否存在userName,服务工作正常我检查过,问题是当我尝试使用ajax函数调用它时结果总是为false在我的情况下表示用户名未被使用且可用...所以这是我的服务代码

[ScriptService]
public class RegisterationService : System.Web.Services.WebService
{
    private SqlConnection con;
    //this function will call the sql procedure
    [WebMethod]
    [ScriptMethod]
    public string UserNameExist(string userName)
    {
        bool userNameInUse = false;
        Connection();
        SqlCommand com = new SqlCommand("UserNameExists", con);
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.Add(new SqlParameter()
        {
            ParameterName = "@UserName",
            Value = userName
        });

        con.Open();
        userNameInUse = Convert.ToBoolean(com.ExecuteScalar());

        Registeration reg = new Registeration();
        reg.UserName = userName;
        reg.UserNameInUse = userNameInUse;

        //to serialize this to JSON
        JavaScriptSerializer js = new JavaScriptSerializer();
        //Context.Response.Write(js.Serialize(reg));           
        con.Close();
        return js.Serialize(reg);
    }
    private void Connection()
    {
        string constr = ConfigurationManager.ConnectionStrings["NerdsContext"].ToString();
        con = new SqlConnection(constr);
    }
}

这是我的ajax脚本:

 $(document).ready(function () {
                $("#txtUserName").keyup(function () {
                    var userNameVal = document.getElementById("txtUserName").value;
                    if (userNameVal.length >= 3)
                    {
                        $.ajax({
                            url: '../RegisterationService.asmx/UserNameExist',
                            contentType: "application/json; charset=utf-8",
                            method: 'POST',
                            data: JSON.stringify({'userName': userNameVal}),
                            //the data the server expecting
                            dataType: 'json',

                            success: function (data) {
                                var divElement = $("#divOutput");
                                if (data.userNameInUse)
                                {
                                    divElement.css('color', 'red');
                                    divElement.text(userNameVal + ' is already in use');  

                                }
                                else 
                                {
                                    divElement.css('color', 'green');
                                    divElement.text(userNameVal + ' is available');

                                }
                                //alert(data.userName);

                            },
                            error: function (xhr, ajaxOptions, thrownError) {
                                console.log(xhr.status);
                                console.log(xhr.responseText);
                                console.log(thrownError);
                            }


                        });
                    }
                });
});

我不知道问题出在哪里。

1 个答案:

答案 0 :(得分:1)

con.Close();
var obj = new Registration
{
    Username = userName,
    UserNameInUse = userNameInUse
};
var json = new JavaScriptSerializer().Serialize(obj);