• 一个简单的AJAX实现(HELLO AJAX)


    客户端部分:

    <html>

    <head>
        
    <meta http-equiv="Content-Type" content="text/html"/>
        
    <script language="javascript">
            
    var ajax;
            
    function createAjax()
            {
                
    if(window.ActiveXObject)
                {
                    
    try
                    {
                        
    return new ActiveXObject("Msxm12.XMLHTTP");
                    }
                    
    catch(e)
                    {
                        
    try
                        {
                            
    return new
                                    ActiveXObject(
    "Microsoft.XMLHTTP");
                        }
                        
    catch(e2)
                        {
                            
    return null;
                        }
                    }
                }
                
    else if(window.XMLHttpRequest)
                {
                    
    return new XMLHttpRequest();
                }
                
    else
                {
                    
    return null;
                }
            }
            
    function onRcvData()
            {
                
    if(ajax.readyState==4)
                {
                    
    if(ajax.status==200)
                    {
                        
    var content=document.getElementById('content');
                        content.innerHTML
    =ajax.responseText;
                    }
                    
    else
                    {
                        alert(
    "error");
                    }
                }
            }
            
    function ajaxSendRequest(uri)
            {
                ajax
    =createAjax();
                
    if(!ajax)
                {
                    alert(
    "no");
                    
    return 0;
                }
                
                ajax.onreadystatechange
    =onRcvData;
                ajax.open(
    "GET",uri,true);
                ajax.send(
    "");
            }
        
    </script>
    <title>Hello AJAX</title>

    </head>

    <body>
        
    <div id="content"></div>
        
    <br>
        
    <input type="button" value="Hello" 
            onclick
    ="ajaxSendRequest('http://localhost:8080/test/hello.jsp')">
    </body>

    </html>

    服务器端部分(hello.jsp)

    <html>
        
    <head>
            
    <title>hellp</title>
        
    </head>
        
    <body>
           
    <%
              out.println(
    "HELLO AJAX");
           
    %>
        
    </body>
    </html>
  • 相关阅读:
    J2EE13个规范--【J2EE】
    事件监听、持有对方的引用--【J2SE】
    TCP协议:服务端和客户端demo--【J2SE】
    线程:Interrupt、Sleep、Join、线程同步--【J2SE】
    1. Visual C++ 6.0 安装和使用
    无法连接虚拟设备sata0:1,因为主机上没有相应的设备
    Linux文件夹文件创建、删除
    服务器
    linux 下 apache启动、停止、重启命令
    Apache部署静态html
  • 原文地址:https://www.cnblogs.com/xpxu/p/1683844.html
Copyright © 2020-2023  润新知