Hello,Ajax

学习了Ajax技术,写了一个最简单的Ajax应用

<%@page contentType="text/html; charset=utf-8" language="java" import="java.sql.* " errorPage="" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'myHelloWorld.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    <script type="text/javascript">
        function ok(){
            var xmlhttp;
            if(window.XMLHttpRequest){
                //非ie
                xmlhttp=new XMLHttpRequest();
            }else if(window.ActiveXObject){
                try{
                    //ie
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
            }
            //设定回调函数
            xmlhttp.onreadystatechange=function(){
                if(xmlhttp.readyState==4){
                    if(xmlhttp.status==200){
                        //获取返回值
                        var msgWelcome=xmlhttp.responseText;
                        var msg=document.getElementById("msg");
                        msg.innerHTML=msgWelcome;
                    }
                }
            }
            //设定请求
            xmlhttp.open("get", "http://localhost:8080/LearnJava/myHelloWorld.do", true);
            //设定http头
            xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
            //发送请求
            xmlhttp.send(null);
        }
    </script>
  </head>
  
  <body>
    <span id="msg"></span><br>
    <input type="button" onclick="ok()" value="单击">
  </body>
</html>
View Code

关键代码:

<script type="text/javascript">
        function ok(){
            var xmlhttp;
            if(window.XMLHttpRequest){
                //非ie
                xmlhttp=new XMLHttpRequest();
            }else if(window.ActiveXObject){
                try{
                    //ie
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e){
                    xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
                }
            }
            //设定回调函数
            xmlhttp.onreadystatechange=function(){
                if(xmlhttp.readyState==4){
                    if(xmlhttp.status==200){
                        //获取返回值
                        var msgWelcome=xmlhttp.responseText;//获取response的输出值
                        var msg=document.getElementById("msg");
                        msg.innerHTML=msgWelcome;
                    }
                }
            }
            //设定请求
            xmlhttp.open("get", "http://localhost:8080/LearnJava/myHelloWorld.do", true);
            //设定http头
            xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
            //发送请求
            xmlhttp.send(null);
        }
    </script>

这里只是一个简单的ajax的应用,将servlet输出信息写入页面中。过程是:

首先创建一个xmlhttp对象,然后以get方法向servlet发送请求。当

onreadystatechange事件发生,则调用回调函数,向界面输出信息。

转载于:https://www.cnblogs.com/superxuezhazha/p/6340513.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值