其他分享
首页 > 其他分享> > AJAX概念和AJAX实现_原生JS方式

AJAX概念和AJAX实现_原生JS方式

作者:互联网

AJAX概念:

实现方式:

package com.ailyt.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ajaxServlet", value = "/ajaxServlet")
public class AjaxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");

        System.out.println(username);

        response.getWriter().println(username);


    }
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <script>
        $(function () {

        });

        function fun() {
        //发送异步请求
        //创建核心对象
        var xmlhttp;
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XML HTTP");
            }

        //    建立连接
            /**
             * 参数:
             * 请求方式 get post
             *  get:请求参数在url后拼接,send方法为空参
             *  post:请求参数在send方法中定义
             * 请求的url
             * 同步或异步请求 true 异步  false同步
             */
            xmlhttp.open("GET","ajaxServlet?username=tom",true)
        //发送请求
            xmlhttp.send();

        //    接受并响应来自服务器的响应结果
        }
    </script>
</head>
<body>
    <input type="button" value="发送异步请求" onclick="fun();"/>
</body>
</html>

标签:原生,异步,xmlhttp,请求,JS,username,AJAX,网页,客户端
来源: https://www.cnblogs.com/ailhy/p/16607670.html