网站制作学习网ASP→正文:ajax实例
字体:

ajax实例

ASP 2009/11/2 9:24:36  点击:不统计

ajax实例,对ajax进行了简单化的展示,新手都能看懂
分为5个文件,来实现ajax,简单方便 下载地址:ajax实例下载
首先是 ajax.js javascript文件(www.forasp.cn网站制作学习网原创)代码如下:
var xmlobj;
var textname;
function ajaxtest(title)//ajax主程序函数参数可以是多个,
{
CreateXMLHttpRequest(); //创建对象
var showurl = "checkid_action.asp?title="+title;//构造URL
xmlobj.open("GET", showurl, true);//调用
xmlobj.onreadystatechange = StatHandler;//判断URL调用的状态值并处理
xmlobj.send(null);//设置为不发送给服务器任何数据
}
function StatHandler()//用于处理状态的函数
{if(xmlobj.readyState == 4 && xmlobj.status == 200)                                                                        
{array_str = xmlobj.responseText;//可以返回多个参数,.split("#")用这个活着其他符号分开通过数组判断
if(array_str == 1){
document.getElementById("action_result").innerHTML = "<font color = 'green'>没有记录</font>";
document.getElementById("flag").disabled = false;}
else if(array_str == 2){
document.getElementById("action_result").innerHTML = "<font color = 'red'>拥有记录</font>";
document.getElementById("flag").disabled = true;
}}}
function CreateXMLHttpRequest()//获取浏览器类型并通过判断创建对象
{if(window.XMLHttpRequest)
{//Mozilla浏览器
xmlobj=new XMLHttpRequest();
if(xmlobj.overrideMimeType)
{//设置MIME类别
xmlobj.overrideMimeType("text/xml");
}}else if(window.ActiveXObject)
{//IE浏览器
try
{xmlobj=new ActiveXObject("Msxml2.XMLHttp");}
catch(e)
{try
{xmlobj=new ActiveXobject("Microsoft.XMLHttp");}catch(e)
{}}}}
ajax动作页面是checkid_action.asp,(forasp原创),代码如下
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%option explicit%>
<!--#include file="conn.asp"-->
<%
Response.Buffer = True
Response.ExpiresAbsolute = Now() - 1
Response.Expires = 0
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "No-Cache"
dim title,sql,rs
title = trim(request.QueryString("title"))
if title="" then
response.Write "2"
else
sql = "select * from content where title = '"&title&"'"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,1
if rs.eof or rs.bof then
response.Write "1"
else
response.Write "2"
end if
rs.close
set rs = nothing
end if
%>
首页展示页面index.asp代码如下:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%option explicit%>
<!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>首页--展示所有的数据</title>
<!--#include file="conn.asp"--><!--数据库链接文件-->
<style type="text/css">
<!--
body,td,th {
font-size: 14px;
color: #000000;
}
-->
</style>
</head>
<script type="text/javascript" src="ajax.js"></script>
<body>
<form action="index_action.asp"  method="post">
<table width="635" border="0" cellpadding="0" cellspacing="0">
<tr>
<td colspan="3" align="center">添加数据</td>
</tr>
<tr>
<td width="95">标题:</td>
<td width="377"><input type="text" name="title" onblur="javascript:ajaxtest(this.value);" /></td>
<td width="163"><span id="action_result"></span></td>
</tr> <tr>
<td colspan="3" align="center"><input type="submit" value="确定添加"  id="flag"></td>
</tr>
</table>
</form>
<br />
<table width="647" border="1">
<tr>
<td width="165">序号</td>
<td width="466">标题</td>
</tr>
<%
dim sql,rs
sql = "select * from content order by id desc"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,Conn,1,1
if not rs.eof and not rs.bof then
do while not rs.eof
%>
<tr>
<td><%response.Write rs("id")%></td>
<td><%response.Write rs("title")%></td>
</tr>
<%rs.movenext
  loop
end if
rs.close
set rs =nothing
conn.close
%>
</table></body>
</html>
首页添加内容页面index_action.asp
网站制作学习网
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%option explicit%>
<!--#include file="conn.asp"-->
<%
dim sql,rs,title
title = request.Form("title")
sql ="select * from content"
set rs=server.CreateObject("adodb.recordset")
rs.open sql,conn,1,3
rs.addnew
rs("title") = title
rs.update
rs.close
set rs = nothing
response.Redirect "index.asp"
%>
数据库连接页面conn.asp
<%
Dim Data_Path,ConnStr,Conn
Data_Path="date.mdb"'数据库文件路径
ConnStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source = " & Server.MapPath(Data_Path)
Set Conn=Server.CreateObject("Adodb.Connection")
Conn.open ConnStr
%>
网站制作学习网原创,支持转载,但请保留连接http://www.forasp.cn
 

·上一篇:asp下载图片代码 >>    ·下一篇:asp强制定义优势 >>
推荐文章
最新文章