asp如何用ajax无刷新技术
展开全部
例如无刷新登录
index.asp
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>企业信息管理系统_用户登录</title>
<script src="Ajax.js" type="text/javascript"></script>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #016aa9;
overflow:hidden;
}
.STYLE1 {
color: #000000;
font-size: 12px;
}
-->
</style></head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><table width="962" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="235" background="images1/login_01.gif"> </td>
</tr>
<tr>
<td height="53"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="394" height="53" background="images1/login_05.gif"> </td>
<td width="206" background="images1/login_06.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="16%" height="25"><div align="right"><span class="STYLE1">用户</span></div></td>
<td width="57%" height="25"><div align="center">
<input name="tname" type="text" id="tname" style="width:105px; height:17px; background-color:#292929; border:solid 1px #7dbad7; font-size:12px; color:#6cd0ff">
</div></td>
<td width="27%" height="25"> </td>
</tr>
<tr>
<td height="25"><div align="right"><span class="STYLE1">密码</span></div></td>
<td height="25"><div align="center">
<input name="tpwd" type="password" id="tpwd" style="width:105px; height:17px; background-color:#292929; border:solid 1px #7dbad7; font-size:12px; color:#6cd0ff">
</div></td>
<td height="25"><div align="left"><img src="images1/dl.gif" width="49" height="18" border="0" style="cursor:hand" onClick="useAjaxDemo();"></div></td>
</tr>
</table></td>
<td width="362" background="images1/login_07.gif"> </td>
</tr>
</table></td>
</tr>
<tr>
<td height="213" background="images1/login_08.gif"> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
Ajax.js
function useAjaxDemo()
{
var ajax=createAjaxObject();
sendToValidata();
function createAjaxObject()
{
var ajax;
try
{
ajax= new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
ajax = new XMLHttpRequest();
}
}
return ajax; } //使用AJAX对象
function sendToValidata()
{
if(!ajax)
{
//document.getElementById("msg").innerText="浏览器不支持使用AJAX技术";
alert("浏览器不支持使用AJAX技术");
}
else if(document.getElementById("tname").value==""){
alert("用户不能为空!");
document.getElementById("tname").focus();
}
else if(document.getElementById("tpwd").value==""){
alert("密码不能为空!");
document.getElementById("tpwd").focus();
}
else
{
var url="Chkadmin.asp?nick="+document.getElementById("tname").value+"&password="+document.getElementById("tpwd").value;
ajax.open("get",url,true);
ajax.setRequestHeader("cache-control","no-cache");
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.onreadystatechange =result;
ajax.send(null);
}
}
function result()
{
if(ajax.readyState==4)
{
var reword=ajax.responseText;
if(reword=="1"){
window.location="manage.asp";
//ajax.open("post","manage.asp",true);
}
else{
alert("用户名或密码不正确!");
document.getElementById("tname").focus();
}
}
}
}
Chkadmin.asp
<%
Response.CharSet = "gb2312"
Response.ContentType = "text/xml"
%>
<!--#include file="../inc/md5.asp"-->
<%dbdns="../"%>
<!--#include file="../inc/conn.asp"-->
<%
dim sql
dim rs
dim from1
dim sq12
dim sq123
dim nick
dim password
nick=replace(trim(request("nick")),"'","")
password=md5(replace(trim(Request("password")),"'",""))
regjm=replace(trim(Request("regjm")),"'","")
txt_check=replace(trim(Request("txt_check")),"'","")
'--------------------企业过期数据检查------------------------
set rs=server.createobject("adodb.recordset")
sql="select * from xyz_yp"
rs.open sql,conn,1,3
do while not rs.eof
if cdate(year(rs("xyz_ktdate"))&"-"&month(rs("xyz_ktdate"))&"-"&day(rs("xyz_ktdate")))<=date() then
rs("xyz_key")=2
else
if rs("xyz_key")=2 then
rs("xyz_key")=1
end if
end if
rs.update
rs.movenext
loop
rs.close
set rs=nothing
'--------------------------------------------
set rs=server.createobject("adodb.recordset")
sql="select * from xyz_admin where xyz_password='"&password&"' and xyz_nick='"&nick&"'"
rs.open sql,conn,1,1
if not(rs.bof and rs.eof) then
if password=rs("xyz_password") then
session("admin")=rs("xyz_nick")
session("adminflag")=rs("xyz_flag")
response.Write("1")
response.End()
else
call Error
end if
else
call Error()
end if
sub Error()
response.Write("登陆失败")
Response.End
end sub
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
只是简单的一个例子
index.asp
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>企业信息管理系统_用户登录</title>
<script src="Ajax.js" type="text/javascript"></script>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #016aa9;
overflow:hidden;
}
.STYLE1 {
color: #000000;
font-size: 12px;
}
-->
</style></head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td><table width="962" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="235" background="images1/login_01.gif"> </td>
</tr>
<tr>
<td height="53"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="394" height="53" background="images1/login_05.gif"> </td>
<td width="206" background="images1/login_06.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="16%" height="25"><div align="right"><span class="STYLE1">用户</span></div></td>
<td width="57%" height="25"><div align="center">
<input name="tname" type="text" id="tname" style="width:105px; height:17px; background-color:#292929; border:solid 1px #7dbad7; font-size:12px; color:#6cd0ff">
</div></td>
<td width="27%" height="25"> </td>
</tr>
<tr>
<td height="25"><div align="right"><span class="STYLE1">密码</span></div></td>
<td height="25"><div align="center">
<input name="tpwd" type="password" id="tpwd" style="width:105px; height:17px; background-color:#292929; border:solid 1px #7dbad7; font-size:12px; color:#6cd0ff">
</div></td>
<td height="25"><div align="left"><img src="images1/dl.gif" width="49" height="18" border="0" style="cursor:hand" onClick="useAjaxDemo();"></div></td>
</tr>
</table></td>
<td width="362" background="images1/login_07.gif"> </td>
</tr>
</table></td>
</tr>
<tr>
<td height="213" background="images1/login_08.gif"> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
Ajax.js
function useAjaxDemo()
{
var ajax=createAjaxObject();
sendToValidata();
function createAjaxObject()
{
var ajax;
try
{
ajax= new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
ajax = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
ajax = new XMLHttpRequest();
}
}
return ajax; } //使用AJAX对象
function sendToValidata()
{
if(!ajax)
{
//document.getElementById("msg").innerText="浏览器不支持使用AJAX技术";
alert("浏览器不支持使用AJAX技术");
}
else if(document.getElementById("tname").value==""){
alert("用户不能为空!");
document.getElementById("tname").focus();
}
else if(document.getElementById("tpwd").value==""){
alert("密码不能为空!");
document.getElementById("tpwd").focus();
}
else
{
var url="Chkadmin.asp?nick="+document.getElementById("tname").value+"&password="+document.getElementById("tpwd").value;
ajax.open("get",url,true);
ajax.setRequestHeader("cache-control","no-cache");
ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
ajax.onreadystatechange =result;
ajax.send(null);
}
}
function result()
{
if(ajax.readyState==4)
{
var reword=ajax.responseText;
if(reword=="1"){
window.location="manage.asp";
//ajax.open("post","manage.asp",true);
}
else{
alert("用户名或密码不正确!");
document.getElementById("tname").focus();
}
}
}
}
Chkadmin.asp
<%
Response.CharSet = "gb2312"
Response.ContentType = "text/xml"
%>
<!--#include file="../inc/md5.asp"-->
<%dbdns="../"%>
<!--#include file="../inc/conn.asp"-->
<%
dim sql
dim rs
dim from1
dim sq12
dim sq123
dim nick
dim password
nick=replace(trim(request("nick")),"'","")
password=md5(replace(trim(Request("password")),"'",""))
regjm=replace(trim(Request("regjm")),"'","")
txt_check=replace(trim(Request("txt_check")),"'","")
'--------------------企业过期数据检查------------------------
set rs=server.createobject("adodb.recordset")
sql="select * from xyz_yp"
rs.open sql,conn,1,3
do while not rs.eof
if cdate(year(rs("xyz_ktdate"))&"-"&month(rs("xyz_ktdate"))&"-"&day(rs("xyz_ktdate")))<=date() then
rs("xyz_key")=2
else
if rs("xyz_key")=2 then
rs("xyz_key")=1
end if
end if
rs.update
rs.movenext
loop
rs.close
set rs=nothing
'--------------------------------------------
set rs=server.createobject("adodb.recordset")
sql="select * from xyz_admin where xyz_password='"&password&"' and xyz_nick='"&nick&"'"
rs.open sql,conn,1,1
if not(rs.bof and rs.eof) then
if password=rs("xyz_password") then
session("admin")=rs("xyz_nick")
session("adminflag")=rs("xyz_flag")
response.Write("1")
response.End()
else
call Error
end if
else
call Error()
end if
sub Error()
response.Write("登陆失败")
Response.End
end sub
rs.close
conn.close
set rs=nothing
set conn=nothing
%>
只是简单的一个例子
展开全部
// 使用jquery库
// 客户端的脚本
function ajaxsave(id){
var url,data;
url="server.asp";//这里是你的服务器文件
data={action:"isread",id:id};
$.post(url,data,function(result){
if(result=="1"){
document.getElementById('obj').value="已读";
}
else{
document.getElementById('obj').value="未查看";
}
});
}
//========下面是server.asp页面代码=============
dim action
action=request.form("action")
if action="isread" then
call DoRead()
end if
sub DoRead()
dim id
id=request.form("id")
// 这里添加你执行的代码
if true then // 已读
response.write("1")
else
response.write("-1")
end if
end sub
// 客户端的result就在你server.asp的respose.write的内容
// 客户端的脚本
function ajaxsave(id){
var url,data;
url="server.asp";//这里是你的服务器文件
data={action:"isread",id:id};
$.post(url,data,function(result){
if(result=="1"){
document.getElementById('obj').value="已读";
}
else{
document.getElementById('obj').value="未查看";
}
});
}
//========下面是server.asp页面代码=============
dim action
action=request.form("action")
if action="isread" then
call DoRead()
end if
sub DoRead()
dim id
id=request.form("id")
// 这里添加你执行的代码
if true then // 已读
response.write("1")
else
response.write("-1")
end if
end sub
// 客户端的result就在你server.asp的respose.write的内容
追问
能把你做好的一个发给我吗?这是我的邮箱34758832@qq.com
非常感谢!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
使用ajax方法调用,不用form提交
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询