原生ajax

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        function fun(){
            //ajax核心xmlHttpRequest对象
            var xhttp;
            if (window.XMLHttpRequest) {
                xhttp = new XMLHttpRequest();
            } else {
                // code for IE6, IE5
                xhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            //建立连接
                /*
                    async:true(异步)或 false(同步)
                 */
            xhttp.open("get","ser1?uname=lwx",true);

            //发送请求
                /*
                    get:无参  参数在url后
                    post:有参数
                 */
            xhttp.send();

            //接受处理返回结果
                //就绪状态改变时
            xhttp.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    console.log(this.responseText);

                }
            };
        }
    </script>
</head>
<body>
<!--    原生ajax-->
        <button type="button" value="ajax1" onclick="fun()">btn</button>
        <input>
</body>
</html>
package com.xxx;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ser1")
public class ser1 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uname=req.getParameter("uname");
        System.out.println(uname);
        resp.getWriter().write(uname);
    }
}
posted @   lwx_R  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示