龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Javascript编程 >

AJAX技术 vs 传统的ASP无刷新技术

时间:2013-03-06 14:58来源:未知 作者:admin 点击:
分享到:
Win98系统:c:Windows c:Windowssystem Winnt和Win2000系统:c:Winntc:Winntsystem32 Winxp系统:c:Windows c:Windowssystem32 ajax技术的优势到底在哪里呢,不明白。看一个最简单的例子,假设用户注册的时候,判

    Win98系统:c:Windows  c:Windowssystem
    Winnt和Win2000系统:c:Winntc:Winntsystem32
    Winxp系统:c:Windows  c:Windowssystem32    
ajax技术的优势到底在哪里呢,不明白。看一个最简单的例子,假设用户注册的时候,判断用户输入的名字是否已经被占用,假设输入数字1:显示"OK"(表示正确,可以使用);输入其他字符:显示"Error" (表示错误,已经被占用)。

用ajax技术实现

代码: t.htm

<script>
function f(){
var req =new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET","t.asp?t1="+t1.value, true);
req.onreadystatechange = function(){
  if (req.readyState == 4) {
  if (req.status == 200) {
msg.innerText = req.responseXML.documentElement.tagName;
}
}
}
req.send(null);
}
</script>
<div>1:显示"OK"; 其他字符:显示"Error"</div>
<input id=t1 value=1>
<input type=button value="检测" onclick="javascript:f()">
<div id=msg></div>

代码:

t.asp

<% 
Response.ContentType="text/xml"
if request.querystring("t1")="1" then
response.write("<OK/>")
else
response.write("<ERROR/>")
end if
%>

传统的asp无刷新技术

代码t.htm

<script>
function f(){
document.getElementById("o").src="t.asp?t1="+t1.value
}
</script>
<div>1:显示"OK"; 其他字符:显示"Error"</div>
<input id=t1 name=1 value=1>
<input type=button value="检测" onclick="javascript:f()">
<div id=msg></div>
<iframe src="t.asp" style="display:none" id=o></iframe>

代码t.asp

<% 
if request.querystring="" then response.end
ss="ERROR"
if request.querystring("t1")="1" then ss="OK"
%>
<script>parent.msg.innerText="<%=ss%>"</script>

两者都是无刷新提取服务器(数据库)数据并实时显示在客户段,那为什么现在都要用 ajax呢,还要考虑中文编码等麻烦问题。ajax到底好在哪里,请高手详细说说,我可是一知半解,还没有领会到 ajax的妙处。

在这里,我们暂时只讨论 ajax 调用同域下的asp文件(也就是说自己写的asp程序),小偷程序,偷取新闻,天气预报等应用技术暂时不讨论
,例子写得很简,没有进行错误处理等,仅为讨论之用。

(责任编辑:海纳百川  qlmzl11268@hotmail.com   TEL:(010)68476606-8007)


精彩图集

赞助商链接