qq_30777943
ChinaTrojangirl
采纳率50%
2017-07-31 09:21 阅读 807

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条回答 默认 最新

  • showbo GoCityPass新加坡曼谷通票 2017-07-31 09:28
      data: {"name":$("#name").val()}
     ==>
      data: JSON.stringify({"name":$("#name").val()})
    
    点赞 评论 复制链接分享

相关推荐