An easy example of using AJAX

转载 2006年05月23日 13:50:00

If you are new to this topic , this file may help you to get close to AJAX.

ok, here  I use asp just to do the first test example of this topic.

We have three files here, listed  as:

reg.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>TEST AJAX</title>

<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="reg.js"></script>

</head>
<body>

<div id="msg"></div>
ID: <input type="text" name="regid"><br>
Password: <input type="password" name="regpassword"><br>
<input type="button" id="regsubmit" onclick="Check()" value="Submit">

</body>
</html>

reg.js

//获取页面指定ID公用函数
function GE(a){return document.getElementById(a);}


//表单检测
function Check(){
 if(GE('regid').value==''){GE('msg').innerHTML='ID不能为空';return false}
 //检测PASSWORD是否为空
 if(GE('regpassword').value==''){GE('msg').innerHTML='password 不能为空';return false}
//检测OK后提交数据
 
//建立XMLHttpRequest对象
var X=new ActiveXObject("Microsoft.XMLHTTP");

//检测浏览器是否支持XMLHttpRequest
if(X){
//禁止客户端再次提交表单
  GE('regsubmit').disabled='true';
//onreadystatechange为XMLHttpRequest的状态改变的事件触发器
    X.onreadystatechange=function(){
 
//readyState 对象状态
//0 = 未初始化
//1 = 读取中
//2 = 已读取
//3 = 交互中
//4 = 完成
      if(X.readyState==4){
//交互完成的处理
//status,服务器返回的状态码, 200为成功
        if(X.status==200){
//运行服务器返回的脚本
        eval(X.responseText)
        }
//服务器端程序运行失败,返回错误代码
        else{GE('msg').innerHTML=X.statusText}
      }
    };
//获取服务器端数据
//open("method","URL"[,asyncFlag])
//请求的目标 URL, 方法
//采用POST为提交数据
//采用true为异步传输, false为同步传输
 
    X.open('POST','reg.asp',true);
    X.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    var SendData = 'regid='+GE('regid').value+'&regpassword='+GE('regpassword').value
    X.send(SendData)
  }
  //不支持的话返回错误提示
  else{
    GE('msg').innerHTML='你的浏览器不支持XMLHttpRequest'
  }
}

reg.asp

<%
dim regid, regpassword, str
regid=Request.Form("regid")
regpassword=Request.Form("regpassword")
if regid="" or regpassword="" then
  str = "You must input id and passqord!"
else

  str = "Success,ID is " & regid & " , password is" & regpassword

End if
Response.Write "GE('msg').innerHTML='" & str & "';GE('regsubmit').disabled=false"
Response.End
%>

OVER

相关文章推荐

#150 – An Example of Using PropertyChanged and CoerceValue Callbacks(PropertyChanged和CoerceValue例子)

在使用依赖属性的时候,有的时候可能会遇到需要强制限定多个依赖属性之间的关系的情况,这个时候一般会用CoerceValueCallback 和PropertyChangedCallbacks 来实现。 ...

An example of using libexif to set JPEG Orientation Tag

An example of using libexif to set JPEG Orientation Tag : http://www.jwz.org/blog/2008/12/cocoa-exif...

两个用C实现遗传算法的程序 an example of a very simple genetic algorithm in C and a GA implementation using binary and real coded variables

/* ga.c is an example of a very simple genetic algorithm in C */ & /***************************...

Looking for an example for inserting content into the response using a servlet filter

I’ve been searching the net and stackoverflow for an example of somebody inserting content into the ...

An Example Using boto Amazon MWS Package

Overview This article is a follow up of the Guide to boto Amazon MWS Python Package.Here we give ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)