asp.net 用ajax验证用户名是否已注册,我这写的哪里有问题,求大神指点!!!!
 1. aspx页面代码
$("#name").blur(function () {
                //alert($("#name").val());
                $.ajax({
                    type: "Post",
                    url: "help.aspx/CheckMember",
                    data: {"name":$("#name").val()},
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (res) {
                        alert(res.d);
                    },
                    error: function (err) {
                        alert(err);
                    }
                });
            });
2.help.aspx后台代码
[WebMethod]
    public string CheckMember(string name)
    {
        string sql = string.Format("select * from stuInfo where stuName='{0}'", name);
        SqlConnection conn = new SqlConnection(connstring);
        SqlCommand com = new SqlCommand(sql, conn);
        try
        {
            conn.Open();
            com.ExecuteScalar();
            if (com.ExecuteScalar() == null)
            {
                return "";
            }
            else
            {
                return "该用户名已被注册";
            }
        }
        catch (Exception ex)
        {
            return ex.ToString();
        }
        finally
        {
            conn.Close();
        }
    }

1个回答

  data: {"name":$("#name").val()}
 ==>
  data: JSON.stringify({"name":$("#name").val()})
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!