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>
相关推荐
wdfk_prog18 分钟前
[Linux]学习笔记系列 -- [arm][debug]
linux·运维·arm开发·笔记·学习
wdfk_prog1 小时前
实战教程:从“对象文件为空“到仓库重生——修复 Git 仓库损坏全记录
大数据·网络·笔记·git·学习·elasticsearch·全文检索
小杨爱搞嵌入式2 小时前
【STM32】GPIO的输入输出
c语言·笔记·stm32·单片机·学习
杨荧2 小时前
基于大数据的美食视频播放数据可视化系统 Python+Django+Vue.js
大数据·前端·javascript·vue.js·spring boot·后端·python
皮蛋sol周2 小时前
嵌入式学习硬件(一)ARM体系架构
arm开发·学习·架构
cmdyu_2 小时前
如何解决用阿里云效流水线持续集成部署Nuxt静态应用时流程卡住,进行不下去的问题
前端·经验分享·ci/cd
WordPress学习笔记2 小时前
根据浏览器语言判断wordpress访问不同语言的站点
前端·javascript·html
yuanmenglxb20042 小时前
解锁webpack核心技能(二):配置文件和devtool配置指南
前端·webpack·前端工程化
JefferyXZF2 小时前
Next.js 路由导航:四种方式构建流畅的页面跳转(三)
前端·全栈·next.js
啃火龙果的兔子3 小时前
React 多语言(i18n)方案全面指南
前端·react.js·前端框架