elva

简单的Ajax程序

<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<TITLE> ajax passport </TITLE>
<style>
body{
font-size:14px;
}
</style>
<SCRIPT LANGUAGE="JavaScript">
<!--
function InitAjax(){
    var http_request = false;
    if (window.XMLHttpRequest){
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType){
            http_request.overrideMimeType('text/xml');
        }
    }else if (window.ActiveXObject){
        try{
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e){
            try{
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e)
            {}
        }
    }
    if (!http_request){
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
    return http_request;
}

function IDRequest(n)
{
 //定义收到服务器的响应后需要执行的JavaScript函数
 document.getElementById('message').innerHTML = '数据发送中,请稍候...';
 var proxy = "proxy.asp?uid="
 url=proxy+document.getElementById('username').value+'&pwd='+document.getElementById('userpwd').value;//定义网址参数
 xmlhttp_request=InitAjax();//调用创建XMLHttpRequest的函数
 xmlhttp_request.open('GET', url, true);
 xmlhttp_request.send(null);
 xmlhttp_request.onreadystatechange = doContents;//调用doContents函数
}


function doContents()
{
 if (xmlhttp_request.readyState == 4)
 {
  // 收到完整的服务器响应
  if (xmlhttp_request.status == 200)
  {
   //HTTP服务器响应的值OK
//   var response = xmlhttp_request.responseXML.documentElement;
//   var result = response.getElementsByTagName('Status')[0].firstChild.data;//返回result节点数据
   var result = xmlhttp_request.responsetext;
   if(result ==0)
   {
    document.getElementById('message').innerHTML = "<span style='color:green'>用户名"+document.getElementById('username').value+" 注册成功</span>";
   }
   else if(result ==1)
   {
    document.getElementById('message').innerHTML = "<span style='color:red'>对不起,用户名"+document.getElementById('username').value+"已经注册</span>";
   }
   else if(result ==2)
   {
    document.getElementById('message').innerHTML = "<span style='color:red'>用户名或密码不能为空</span>";
   }
   //将服务器返回的字符串写到页面中ID为message的区域
  }
  else
  {
   document.getElementById('message').innerHTML = "<span style='color:red'>系统错误!</span>";
   alert(xmlhttp_request.status);
  }
 }
}
function dlnum(){
 var str;
 str=document.all.message.innerHTML;
 str.valueOf();
 if (str=='')
 str=0;
 if (isNaN(str))
 str=0;
 else
 str=Number(str);
 str=str+1;
 document.all.message.innerHTML=str;
}
//-->

</SCRIPT>
</HEAD>

<BODY>

请输入用户名:<input type="text" id="username" />
请输入密码:  <input type="password" id="userpwd" />
<input type="button" id="b1" value="提交" onclick="IDRequest();" />
<A HREF="javascript:;" onclick="dlnum();">点击</A>
<div id="message"></div>

</BODY>
</HTML>
 

<!-- proxy.asp -->

<%
Response.buffer=true
Response.Expires = -1
Response.AddHeader "Pragma","no-cache"
Response.AddHeader "cache-ctrol","no-cache"

Set Conn = Server.CreateObject("ADODB.Connection")
Set Rs = Server.CreateObject("ADODB.Recordset")
ConnSQL = "driver={sql server};database=test;server=.;uid=sa;pwd="
Conn.open ConnSQL
uid = Request.QueryString("uid")
pwd = Request.QueryString("pwd")
Function ResponseText(val)
 Response.Write val
End Function
If Trim(uid) <> "" And pwd <> "" Then
 Sql = "Select * From test..test Where aa='"&uid&"'"
 Rs.Open Sql,Conn,1,3
 If Rs.Eof And Rs.Bof Then
  Sql = "Insert Into test..test values('"&uid&"','"&pwd&"')"
  Conn.Execute Sql
  ResponseText("0")
 Else
  ResponseText("1")
 End If
 Rs.Close
Else
 ResponseText("2")
End If
Conn.Close
%>

posted on 2007-05-28 19:50 叶子 阅读(381) 评论(0)  编辑 收藏 引用 所属分类: Ajax


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理