JavaWeb前端-Ajax

简介

Asynchronous JavaScript And XML,异步的JavaScript和XML

XML:Extensible Markup Language,可扩展标记语言,是一种数据格式,可用来存储复杂的数据结构

Ajex作用:

  • 数据交换:可给服务器发送请求,获取服务器响应的数据
  • 异步交互:可在不重新加载整个页面 的情况下,与服务器交换数据并更新部分网页的技术

同步和异步

Axios

对原生Ajax进行了封装,简化书写
Axios官网

步骤:

  • 引入Axios的js文件
  • 使用Axios发送请求,并获取响应结果
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax-Axios</title>
</head>
<body>
    <input type="button" value="get" id="btnget">
    <input type="button" value="post" id="btnpost">

    <script src="js/axios.js">
    </script>
    <script>
        //1、get请求
        document.querySelector("#btnget").addEventListener("click",function(){
            //axios发起异步请求
            axios({
                method:"get",
                url:"http://127.0.0.1:3000/get",
            }).then((res) =>{ //成功回调函数
                console.log(res);
            }).catch((err) =>{ //失败回调函数
                console.log(err);
            })
        })
        //2、post请求
        document.querySelector("#btnpost").addEventListener("click",function(){
            //axios发起异步请求
            axios({
                method:"post",
                url:"http://127.0.0.1:3000/post",
                data:{username:"admin",password:"<PASSWORD>"},//post请求方式,请求体的数据
            }).then((res) =>{ //成功回调函数
                console.log(res);
            }).catch((err) =>{ //失败回调函数
                console.log(err);
            })
        })
    </script>
</body>
</html>

请求方式别名

格式:axios.请求方式(url [, data [, config]])

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Ajax-Axios</title>
</head>
<body>
    <input type="button" value="get" id="btnget">
    <input type="button" value="post" id="btnpost">

    <script src="js/axios.js">
    </script>
    <script>
        //1、get请求
        document.querySelector("#btnget").addEventListener("click",function(){
            axios.get("http://127.0.0.1:3000/get").then((res) =>{ //成功回调函数
                console.log(res);
            }).catch((err) =>{ //失败回调函数
                console.log(err);
            })
        })
        //2、post请求
        document.querySelector("#btnpost").addEventListener("click",function(){
            axios.post("http://127.0.0.1:3000/post",{username:"admin",password:"<PASSWORD>"}).then((res) =>{ //成功回调函数
                console.log(res);
            }).catch((err) =>{ //失败回调函数
                console.log(err);
            })
        })
    </script>
</body>
</html>

async & await

可通过async和await把异步变为同步操作。async声明一个异步方法,await等待异步任务执行

前端向服务器端请求搜索数据的方法

html 复制代码
<script src="js/axios.js"></script>
    <script type="module">
        import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
        createApp({
            data() {
                return {
                    searchForm:{//封装用户输入的查询条件
                        name: '',
                        gender: '',
                        position: ''
                    },
                    empList: []
                }
            },
            methods: {
                async search(){
                    //发起Ajax请求,获取数据
                    // axios.get('http://localhost:8080/emps/list?name=${this.searchForm.name}&gender=${this.searchForm.gender}&position=${this.searchForm.position}').then(function (response) {
                    //     //请求成功,将数据保存到 empList 中
                    //     this.empList = response.data.data;
                    // });
                    let result = await axios.get('http://localhost:8080/emps/list?name=${this.searchForm.name}&gender=${this.searchForm.gender}&position=${this.searchForm.position}');
                    this.empList = result.data.data;
                },
                clear() {
                    //清空搜索条件
                    this.searchForm = {
                        name: '',
                        gender: '',
                        position: ''
                    }
                    this.search();
                }
            },
        }).mount('#container');
        </script>

await关键字只在async函数内有效,await关键字取代then函数,等待获取到请求成功的返回值

Vue生命周期

生命周期指一个对象从创建到销毁的整个过程

八个阶段:每触发一个生命周期事件,会自动执行一个生命周期方法(钩子方法)

html 复制代码
<script src="js/axios.js"></script>
    <script type="module">
        import { createApp } from 'https://unpkg.com/vue@3/dist/vue.esm-browser.js';
        createApp({
            data() {
                return {
                    searchForm:{//封装用户输入的查询条件
                        name: '',
                        gender: '',
                        position: ''
                    },
                    empList: []
                }
            },
            methods: {
                async search(){
                    //发起Ajax请求,获取数据
                    // axios.get('http://localhost:8080/emps/list?name=${this.searchForm.name}&gender=${this.searchForm.gender}&position=${this.searchForm.position}').then(function (response) {
                    //     //请求成功,将数据保存到 empList 中
                    //     this.empList = response.data.data;
                    // });

                    let result = await axios.get('http://localhost:8080/emps/list?name=${this.searchForm.name}&gender=${this.searchForm.gender}&position=${this.searchForm.position}');
                    this.empList = result.data.data;
                },
                clear() {
                    //清空搜索条件
                    this.searchForm = {
                        name: '',
                        gender: '',
                        position: ''
                    }
                    this.search();
                }
            },
            //钩子函数
            mounted() {
                //页面加载时,自动执行一次查询
                this.search();
            }
        }).mount('#container');
    </script>
相关推荐
wcy03104 天前
POST为什么会发送两次请求
javascript·ajax
小刘在重生~15 天前
Django|Ajax 入门、JSON 返回、ECharts 图表、文件上传完整笔记
ajax·django·json
零域码客16 天前
一文掌握 jQuery + Flask 全栈核心知识点(前端交互 + 后端接口 + 前后端联调)
前端·python·ajax·flask·jquery·前后端联调·web全栈
2601_9620974819 天前
JavaScript 异步编程
javascript·ajax·回调函数·异步编程·子线程
lv__pf1 个月前
ajax的使用、json、jquery
ajax·json·jquery
豆角焖肉1 个月前
Layui+Layer 弹出层实现 CRUD:AJAX 无刷新新增修改删除后台实战
前端·ajax·layui
小白勇闯网安圈1 个月前
Vue 进阶实战:事件修饰符、表单控制、购物车与 Ajax 请求
前端·vue.js·ajax
鬼手点金1 个月前
Scrapy 网络爬虫框架
爬虫·python·scrapy·ajax·html·json·requsts
云水一下1 个月前
零基础玩转bWAPP靶场(四十六):XSS - Reflected (AJAX/JSON)
web安全·ajax·json·xss·bwapp·reflected
starzy19901 个月前
Spark 核心之自定义累加器以及版本对比变化深度剖析
大数据·ajax·spark