asp.net ajax
<script language="javascript" type="text/javascript"> function CheckUser() { var userName = $("#tbName").val(); $.ajax({ type: "GET", url: "CheckUser.ashx", data: "name=" + userName, dataType: 'html', error: function () { alert("出错"); }, success: function (msg) { if (msg == "hasExist") { $("#divShow").html("<h4>此帐号已经存在</h4>"); } else if (msg == "notExist") { $("#divShow").html("<h4>此帐号可以注册</h4>"); } } }); } </script>
后端:
public void ProcessRequest(HttpContext context) { try { string name = context.Request.QueryString["name"]; string connstr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["connStr"].ConnectionString; MySqlConnection conn = new MySqlConnection(connstr); conn.Open(); MySqlCommand sd = new MySqlCommand("select * from test where name='" + name + "'", conn); MySqlDataAdapter sda = new MySqlDataAdapter(); sda.SelectCommand = sd; DataSet ds = new DataSet(); sda.Fill(ds); context.Response.ContentType = "text/plain"; if (ds != null && ds.Tables[0].Rows.Count > 0) { context.Response.Write("hasExist"); } else { context.Response.Write("notExist"); } } catch (Exception ex) { context.Response.Write("出错:"+ex.Message); } }