- JScript code
var xmlHttp; function createXmlHttpRequest() { if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); if (xmlHttp.overrideMimeType) { xmlHttp.overrideMimeType("text/xml"); } } else if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } } if (!xmlHttp) { window.alert("你的浏览器不支持创建XMLhttpRequest对象"); } return xmlHttp; }function checkname(name) { createXmlHttpRequest(); var url = "Customer/createcustomerAJAX.aspx?Name=" + name; xmlHttp.open("post", url, true); xmlHttp.onreadystatechange = CheckUserNameResult; xmlHttp.send(null); } function CheckUserNameResult() { if (xmlHttp.readyState == 4)//服务器响应状态 { if (xmlHttp.status == 200)//代码执行状态 { if (xmlHttp.responseText == "true") { document.getElementById("img_companyname").src = "../images/正确 .jpg"; } else { document.getElementById("imgflag").src = "../images/不能为空.jpg"; document.getElementById("Text1").style.display = true; } } } }
Customer/createcustomerAJAX.aspx里的代码:
- C# code
checkcompanyname cu = new checkcompanyname(); if (cu.checkcompany(Request.QueryString["Name"].ToString())) { Response.Write("false"); Response.End(); } else { Response.Write("true"); Response.End(); }
连接数据库代码:
- C# code
public bool checkcompany(string name) { string sql = "select * from customers where companyname='"+name+"'"; SqlConnection con = new SqlConnection(SqlHelper.connectionString); con.Open(); SqlCommand com = new SqlCommand(sql,con); SqlDataReader dr = com.ExecuteReader(); if (dr.Read()) { return true; } else { return false; } }
问题:跟踪发现Customer/createcustomerAJAX.aspx里的代码好像没有执行,也就是没有进行用户名的判断
------解决方案--------------------------------------------------------
function checkname(name) {
createXmlHttpRequest();
var url = "Customer/createcustomerAJAX.aspx?Name=" + name;
xmlHttp.open("post", url, true);
xmlHttp.send(null); xmlHttp.onreadystatechange = CheckUserNameResult;
把这个顺序换下,还有Ajax响应页面/createcustomerAJAX.aspx你重新建立下,应该是用createcustomerAJAX.ashx(一般处理程序),不要用ASPX页面,代码也要改下Request.QueryString要改成context.Request.QueryString
------解决方案--------------------------------------------------------
额,你两个文件目录截个图给我看看