• ajax基本常识及get请求方式


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    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%>">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

    <title>This is my JSP 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">
    -->
    <style type="text/css">
    div{
    border: 1px solid red;
    400px;
    height: 300px;
    }
    </style>
    </head>

    <body>
    <input type="button" value="Ajax" onclick="testAjax()">
    <div id="msg"></div>
    </body>
    <script type="text/javascript">

    function testAjax(){
    //document.getElementById("msg").innerHTML = "加载中...";
    //id;
    var request;
    //创建 request对象
    if(window.XMLHttpRequest){ //兼容性
    request = new XMLHttpRequest();
    }else if(window.ActiveXObject){ //针对IE
    request = new ActiveXObject("Msxml2.XMLHTTP");
    }

    //写监听 去check request的状态
    request.onreadystatechange = function(){
    //
    //console.log(request.readyState);
    if(request.readyState == 4){
    //得到 后台写出的数据

    //当加载成功以后
    if(request.status == 200){
    var data = request.responseText;
    document.getElementById("msg").innerHTML = data;
    }else if(request.status == 404){
    document.getElementById("msg").innerHTML = "资源没有找到";
    }else if(request.status == 500){
    document.getElementById("msg").innerHTML = "服务器错误";
    }

    }else{
    document.getElementById("msg").innerHTML = "<img src="images/loading.gif" />";
    }
    };

    //打开请求
    request.open("get", "ajax/ajaxController?name=李四&d="+new Date().getTime());

    //发送数据
    //如果没有数据 则写null 不然 其他浏览器可能会报错
    request.send(null);
    }

    </script>
    </html>

     
     

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    String name = request.getParameter("name");
    System.out.println(name);
    try {
    Thread.currentThread().sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    //int c = 1/0;
    //response.getWriter().write("Hello Ajax!!!");
    response.getWriter().print(new Gson().toJson(new Users("lisi","123123")));
    }

  • 相关阅读:
    Spring RestTemplate 之put、delete请求
    Too many connections解决方案
    各个平台的mysql重启命令
    MySQL出现too many connections(1040)错误解决方法
    EXCEL中,如何引用一个单元格中的数据,作为另一个单元格内容中的一部分?
    [翻译][Java]ExecutorService的正确关闭方法
    MySQL:日期函数、时间函数总结(MySQL 5.X)
    MySQL 获得当前日期时间 函数
    线程本地变量ThreadLocal
    split 分割 字符串(分隔符如:* ^ : | , . ?) 及注意点
  • 原文地址:https://www.cnblogs.com/hwgok/p/5845026.html
Copyright © 2020-2023  润新知