Day17_1--AJAX学习之GET/POST传参

AJAX 简介

AJAX 是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。其实AJAX就可以理解为就是JS。通过AJAX也就实现了前后端分离,前端只写页面,后端生成数据!

现在开始通过实例学习:

1--GET传参

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script>
    function ajax2() {
        //0:获取input内容
        let user = document.querySelector(".user").value;
        let pwd = document.querySelector(".pwd").value;
        //1:创建ajax对象
        let xmlHttpRequest = new XMLHttpRequest();
        //2:配置后端请求--get请求参数用?和&来传参数
        xmlHttpRequest.open("get", "/stage1_war_exploded/ajax2?user="+user+"&pwd="+pwd, true)
        //3:发送请求
        xmlHttpRequest.send();
        //4:监听数据是否请求成功
        xmlHttpRequest.onreadystatechange = function () {
            //5:如果服务器成功解析数据则readyState=4,如果后端成功返回数据则state=200
            if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
                //6:获取后端数据
                let data = xmlHttpRequest.responseText;
                //7:将数据插入前端页面
                document.getElementById("show").innerHTML = data;
            }
        }
    }
    </script>
<body>
<div id="show" style="border: 1px red; width: 200px;height: 100px">
</div>
账号<input type="text" class="user"><br>
密码<input type="text" class="pwd"><br>
<button onclick="ajax2()">提交</button>//当点击按钮后,调用ajax2()方法
</body>
</html>

2--后端生产数据

java 复制代码
@WebServlet("/ajax2")
public class ajax2 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //后端获取get方式的参数就用getParameter
        String user = req.getParameter("user");
        String pwd = req.getParameter("pwd");
        PrintWriter writer = resp.getWriter();
        //将后端数据返回给前端
        writer.write("后端数据来了"+user+"-----"+pwd);
    }
}

3--POST传参

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script>
    function ajax2() {
        //0:获取input内容
        let user = document.querySelector(".user").value;
        let pwd = document.querySelector(".pwd").value;
        //1:创建ajax对象
        let xmlHttpRequest = new XMLHttpRequest();
        //2:配置后端请求
        xmlHttpRequest.open("post", "/stage1_war_exploded/ajax2", true);
        //3:发送请求
        //3_1:post请求必须设置请求头--一个单词都不能错:固定写法
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        //3_2:发送请求和post参数---------------------其他地方和get相同
        xmlHttpRequest.send("user=" + user + "&pwd=" + pwd);
        //4:监听数据是否请求成功
        xmlHttpRequest.onreadystatechange = function () {
            //5:如果服务器成功解析数据则readyState=4,如果后端成功返回数据则state=200
            if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
                //6:获取后端数据
                let data = xmlHttpRequest.responseText;
                //7:将数据插入前端页面
                document.getElementById("show").innerHTML = data;
            }
        }
    }
</script>
<body>
<div id="show" style="border: 1px red; width: 200px;height: 100px">
</div>
账号<input type="text" class="user"><br>
密码<input type="text" class="pwd"><br>
<button onclick="ajax2()">提交</button>
</body>
</html>
相关推荐
最强小杰7 小时前
DeepSeek V4 Flash + Codex CLI 接入教程:原生 Responses API 适配,从配置到跑通收藏这篇就够了(2026)
java·前端·python·ai
静水leo7 小时前
使用 Whistle 拦截接口并模拟后端响应
前端
掘金者阿豪7 小时前
SQL Server数据迁移完成后,凌晨的数据库告警该怎么查?
前端·后端
JudithHuang7 小时前
React 嵌套路由与布局
前端·react.js·arcgis·react·react router
htzyl2068 小时前
前端阶梯——第四十三章、实战二:待办清单To-Do
前端·javascript·html
赵谨言8 小时前
多语种在线语言学习平台LinguaVerse的设计与实现毕业论文
学习
東隅已逝,桑榆非晚8 小时前
算法3.双指针法-快乐数
c++·笔记·学习·算法
li星野8 小时前
Regular and Irregular Nouns — One-Page A4 Notes
学习
摹客9 小时前
【3D设计】界面设计中的3D元素:应用场景+5款工具横评(含Three.js)
前端