在ASP.NET中利用Ajax技术实现无刷新检测用户名,responText无返回值,innerHTML总是显示“正在检测”。
前台js代码:<scriptlanguage="javascript"type="text/javascript">varxmlHttp=false;try{xmlHtt...
前台js代码:<script language="javascript" type="text/javascript">
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
function callServer() {
var u_name = document.getElementById("u_name").value;
if ((u_name == null) || (u_name == "")) return;
var url = "Checkuser.aspx?u_name=" + escape(u_name);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
function updatePage() {
if (xmlHttp.readyState < 4) {
test1.innerHTML="正在检测";
}
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
test1.innerHTML=response;
}
if (test1.innerHTML=="2"){
document.form1.submit.disabled=true;}else{
document.form1.submit.disabled=false;
}
}
</script>
后台Checkuser.aspx代码:protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = -1;
string nam = Request.QueryString["u_name"].ToString();
string sql = "select * from Table_1 where Stu_ID='" + nam + "'";
SqlConnection conn = new SqlConnection("Data Source=PC-201105061805\\SQLEXPRESS;Initial Catalog=Student;Persist Security Info=True;User ID=admin;Password=song");
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
Response.Write("1");
}
else
{
Response.Write("2");
}
Response.End();dr.Close();
} 展开
var xmlHttp = false;
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
xmlHttp = false;
}
}
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
xmlHttp = new XMLHttpRequest();
}
function callServer() {
var u_name = document.getElementById("u_name").value;
if ((u_name == null) || (u_name == "")) return;
var url = "Checkuser.aspx?u_name=" + escape(u_name);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
function updatePage() {
if (xmlHttp.readyState < 4) {
test1.innerHTML="正在检测";
}
if (xmlHttp.readyState == 4) {
var response = xmlHttp.responseText;
test1.innerHTML=response;
}
if (test1.innerHTML=="2"){
document.form1.submit.disabled=true;}else{
document.form1.submit.disabled=false;
}
}
</script>
后台Checkuser.aspx代码:protected void Page_Load(object sender, EventArgs e)
{
Response.Expires = -1;
string nam = Request.QueryString["u_name"].ToString();
string sql = "select * from Table_1 where Stu_ID='" + nam + "'";
SqlConnection conn = new SqlConnection("Data Source=PC-201105061805\\SQLEXPRESS;Initial Catalog=Student;Persist Security Info=True;User ID=admin;Password=song");
conn.Open();
SqlCommand com = new SqlCommand(sql, conn);
SqlDataReader dr = com.ExecuteReader();
if (dr.Read())
{
Response.Write("1");
}
else
{
Response.Write("2");
}
Response.End();dr.Close();
} 展开
2个回答
展开全部
function callServer() {
var u_name = document.getElementById("u_name").value;
if ((u_name == null) || (u_name == "")) return;
var url = "Checkuser.aspx?u_name=" + escape(u_name);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
这里请求的地址需要绝对路径 比如http://www.baidu.com
而不能是相对的
var u_name = document.getElementById("u_name").value;
if ((u_name == null) || (u_name == "")) return;
var url = "Checkuser.aspx?u_name=" + escape(u_name);
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = updatePage;
xmlHttp.send(null);
}
这里请求的地址需要绝对路径 比如http://www.baidu.com
而不能是相对的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询