Javascript Ajax函数总是返回false

Javascript Ajax函数总是返回false,javascript,ajax,web-services,asp.net-mvc-5,Javascript,Ajax,Web Services,Asp.net Mvc 5,我有一个web服务,它可以检查数据库中是否存在用户名,该服务工作得很好,我检查过了,问题是当我试图使用ajax函数调用它时,结果总是错误的,在我的例子中,这表明用户名未被使用,并且可用…这是我的服务代码 [ScriptService] public class RegisterationService : System.Web.Services.WebService { private SqlConnection con; //this function will call th

我有一个web服务,它可以检查数据库中是否存在用户名,该服务工作得很好,我检查过了,问题是当我试图使用ajax函数调用它时,结果总是错误的,在我的例子中,这表明用户名未被使用,并且可用…这是我的服务代码

[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);
                            }


                        });
                    }
                });
});

我不知道问题出在哪里。

好的,我已经给注册对象赋值了,我真的不认为这是答案,我已经检查了服务,它工作正常告诉我你在做什么
Inspect Element->Network->Response
没有什么让人沮丧的事情我不知道它是否与问题有关,但这是我的服务的输出“{”UserName:“salRad”,“UserNameInUse”:true}”的WebService是正确的,只要给我看一张屏幕截图,上面显示了你的
Inspect Element->Network->Response
WOW,好吧,我对这个很陌生,但非常感谢,我按照你说的做了,这就是结果
“{”UserName:“salRad”,“UserNameInUse”:true}”
那么问题是什么呢?它变成了现实,但被视为错误
con.Close();
var obj = new Registration
{
    Username = userName,
    UserNameInUse = userNameInUse
};
var json = new JavaScriptSerializer().Serialize(obj);