选择显示字体大小

揭开ajax神秘面纱

ajax,即"asynchronous javascript and xml"的缩写,可翻译为异步javascriptxml技术。其核心是一个寄宿在浏览器中名为xmlhttprequest的类。通过此类,可以做到无需提交表单就可以实现与服务器的连接;无需刷新整个页面,就可以动态更新页面中一部分的内容。xmlhttprequest通常使用xml作为数据交换的载体,但也可使用其他的载体,如纯文本。简单来说,就是通过xmlhttprequest发送信息给服务器,异步接收服务器处理并返回信息,然后通过javascript动态更新页面的部分内容。

尽管ajax近来非常火爆,但ajax并非新的技术,正如其名所示,只不过是javascript加上xml的技术罢了。而且使用上非常简单。

应用ajax的流程:
1、定义一个事件处理器
2、获取xmlhttprequest,并将事件处理器注册给它
3、与服务器连接
4、发送信息
5、服务器返回处理完毕的信息
6、每当xmlhttprequest的状态发生变化,自动触发事件处理器
7、事件处理器动态更新页面


本文通过一个简单的例子来说明如何在ie6中使用ajax技术。在这例子中,客户端每隔十秒,从服务器端取回一个随机的字符串,在不重新刷新页情况下,自动更新部分页面内容。例子仅用到了两个jsp文件,client.jsp及server.jsp。为了方便说明,我用server.jsp来代替本应作为servlet的server.java

先看client.jsp内容:

<%@page contenttype=&quot;text/html&quot;%>
<%@page pageencoding=&quot;gb2312&quot;%>

<!doctype html public &quot;-//w3c//dtd html 4.01 transitional//en&quot;
   &quot;http://www.w3.org/tr/html4/loose.dtd&quot;>

<html>
    <head>
        <meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=gb2312&quot;>
        <title>ajax demo</title>
        <script language=&quot;javascript&quot;>
            var xmlhttp;
            
            function getgiftfromserver() {
                var url = &quot;http://localhost:8084/ajax/server.jsp&quot;;
                if (window.activexobject) {
                    xmlhttp = new activexobject(&quot;microsoft.xmlhttp&quot;);
                }

                xmlhttp.onreadystatechange = showgift;

                xmlhttp.open(&quot;get&quot;, url, true);
                xmlhttp.send(null);
                
                settimeout(&quot;getgiftfromserver()&quot;,10000);
            }

            function showgift() {
                if (xmlhttp.readystate == 4 xmlhttp.readystate == &quot;complete&quot;) {
                    document.getelementbyid(&quot;output&quot;).innerhtml = &quot;time is for &quot; + xmlhttp.responsetext + &quot;.&quot;;
                }
            }
        </script>
    </head>
    <body onload=&quot;getgiftfromserver()&quot;>

        <h1>ajax例子</h1>

        <div id=&quot;output&quot;></div>
    </body>
</html>


加载页面时,将调用javascript的getgiftfromserver()函数,此函数完成了上面所提的应用ajax的流程中第1至第4步,同时设定了一个每隔十秒自动调用此函数的定时器。而showgift()函数完成所提流程中的第5至第7步。下面详细说明每一步骤。

1、定义事件处理器,此处理器将在服务器端返回数据时自动被触发执行。
    function showgift() {
        if (xmlhttp.readystate == 4 xmlhttp.readystate == &quot;complete&quot;) {
            document.getelementbyid(&quot;output&quot;).innerhtml = &quot;time is for &quot; + xmlhttp.responsetext + &quot;.&quot;;
        }
    }


因为我们这里使用异步,readystate属性用来判断服务器返回信息的状态。其值是一个从0至4的整数,对应于:

    0:对象已创建,但未初始化(未调用open()方法)
    1:对象已创建,但未调用send()方法
    2:已调用send()方法,但status及headers还未可用
    3:已经传回部分数据,但status及headers还未完全可用
    4:已经收到所有数据,可使用所有数据


2、获取xmlhttprequest,并将事件处理器注册给它

注意:要使用xmlhttprequest,需要ie5.0以上的版本。

2.1 取得xmlhttprequest

在ie7.0之前获得xmlhttprequest,使用如下代码:

    if (window.activexobject) {
        xmlhttp = new activexobject(&quot;microsoft.xmlhttp&quot;);
    }


而在ie7.0中:

    if (window.xmlhttprequest) {
       xmlhttp = new xmlhttprequest
    }


2.2 注册事件处理器

xmlhttp.onreadystatechange = showgift;


showgift为传入的方法名,每当xmlhttprequest的状态发生改变时,将执行此方法

3. 与服务器连接并发送

    
xmlhttp.open(&quot;get&quot;, url , true)


其方法签名如下:

    
xmlhttp.open(bstrmethod, bstrurl, varasync, bstruser, bstrpassword)


其中:
    bstrmethod: 连接方式,可用的有get, post, put, or propfind
      bstrurl: 服务器的url
      varasync(可选): 调用是否异步,默认为true(调用立即返回)
    bstruser(可选):用户名,如果url需要验证时附上
    bstrpassword(可选):密码,如果url需要验证时附上

open()方法可以直接打开一个xml文档,并通过xmlhttp的responsexml来读取相应的节点。如下例:

    xmlhttp.open(&quot;get&quot;,&quot;http://localhost/books.xml&quot;, false);
      xmlhttp.send();
      var book = xmlhttp.responsexml.selectsinglenode(&quot;//book&#91;@id='bk101'&#93;&quot;);


4、发送信息

    
xmlhttp.send(null)


其方法签名如下:

    
xmlhttp.send( &#91;varbody&#93;)


    varbody(可选): 可为字符串或xml等数据,可以为null。无返回值

此方法在open()设为异步时,立即返回;在open()设为同步时,必须等到reponse对象从服务器中返回时才返回。

5、服务器返回处理完毕的信息

此时,该是服务器端工作了,server.jsp的代码如下:

<%
    string&#91;&#93; str = new string&#91;&#93; {&quot;love&quot;, &quot;power&quot;, &quot;peace&quot;};
    int number = new java.util.random().nextint(2+1);
    response.getwriter().write(str&#91;number&#93;);
%>


从三个字符串中随机挑选一个写入到response中,返回客户端。

6、客户端自动探知xmlhttprequest的状态已经发生变化,自动触发事件处理器

7、事件处理器动态更新页面

处理器读取xmlhttp.responsetext的值,并通过javascript动态更新<div id=&quot;output&quot;>的内容。

    
document.getelementbyid(&quot;output&quot;).innerhtml = &quot;time is for &quot; + xmlhttp.responsetext + &quot;.&quot;;



结语:

由上7步可见,ajax并不复杂,远比想像中要简单得多。牢牢记住这一点,&ldquo;ajax让我们在不用刷新页面的情况下,可以动态地更新网页部分内容&rdquo;,然后运用到各种需要用到这种性能的场合,将使我们的网页更有创意。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons