Vue(第十七课)AXIOS对JSON数据的增删改查二)

简介: Vue(第十七课)AXIOS对JSON数据的增删改查二)

4 修改一条记录

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04 获取个人信息</title>
</head>
<style>
    p {
        text-align: center;
        font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
        font-weight: 800;
        width: 100%;
        height: 20px;
        background-color: rgb(255, 0, 89);
        color: rgb(255, 255, 255);
    }
    button {
        width: 300px;
        height: 40px;
        line-height: 40px;
        font-size: 20px;
        border-radius: 12px;
        color: azure;
        background-color: black;
        box-shadow: 1px 2px 3px rgb(255, 25, 0);
    }
    input {
        color: rgb(0, 0, 0);
        font-size: 20px;
        background-color: rgb(255, 255, 255);
        box-shadow: 1px 2px 3px rgb(255, 25, 0);
        border-radius: 12px;
    }
</style>
<body>
    <script src="../js/axios.min.js"></script>
    <p>修改一个人的信息</p>
    <button id="btn4">点我更新一个人的基本数据信息</button><br> <br>
    <input type="text" placeholder="请输入要修改的id编号" id="person_update_id"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp;
    <input type="text" placeholder="请输入要修改的姓名" id="person_update_name"> <br> <br>
    <input type="text" placeholder="请输入要修改的性别" id="person_update_sex"> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp;
    <input type="text" placeholder="请输入要修改的爱好" id="person_update_subject"> <br> <br>
    <script>
        // 修改数据
        const btn4 = document.getElementById('btn4')
        const personUpdateId = document.getElementById('person_update_id')
        const personUpdateName = document.getElementById('person_update_name')
        const personUpdateSex = document.getElementById('person_update_sex')
        const personUpdateSubject = document.getElementById('person_update_subject')
        // 修改id
        btn4.onclick = () => {
            // 修改数据  http://localhost:3000/teachers
            axios({
                url: "http://localhost:3000/teachers",
                method: 'PUT',
                data: {
                    id: personUpdateId.value,
                    name: personUpdateName.value,
                    sex: personUpdateSex.value,
                    subject: personUpdateSubject.value
                }
            }).then(
                response => { console.log("请求成功", response.data) },
                error => { console.log("请求失败", error) }
            )
        }
    </script>
</body>
</html>

5 删除一条记录

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>04 获取个人信息</title>
</head>
<style>
    p {
        text-align: center;
        font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
        font-weight: 800;
        width: 100%;
        height: 20px;
        background-color: rgb(255, 0, 89);
        color: rgb(255, 255, 255);
    }
    button {
        width: 300px;
        height: 40px;
        line-height: 40px;
        font-size: 20px;
        border-radius: 12px;
        color: azure;
        background-color: black;
        box-shadow: 1px 2px 3px rgb(255, 25, 0);
    }
    input {
        color: rgb(0, 0, 0);
        font-size: 20px;
        background-color: rgb(255, 255, 255);
        box-shadow: 1px 2px 3px rgb(255, 25, 0);
        border-radius: 12px;
    }
</style>
<body>
    <script src="../js/axios.min.js"></script>
    <p>删除一个人的信息</p>
    <button id="btn5">点我删除一个人的信息</button>
    <input type="text" id="person_delete_id" placeholder="请输入删除id的编号">
    <script type="text/javascript">
        btn5.onclick = () => {
            axios({
                url: `http://localhost:3000/teachers/${personDeleteId.value}`,
                method: 'DELETE',
            }).then(
                response => { console.log("请求成功", response.data) },
                error => { console.log("请求失败", error) }
            )
        }
    </script>
</body>
</html>

6 axios中常用到的配置项

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>06_axios中常用到的配置项</title>
</head>
<body>
    <script src="../js/axios.min.js"></script>
    <link rel="stylesheet" href="../css/index.css">
    <button id="btn">点击我获取所有的人员信息</button>
    <script>
        // 给axios配置默认属性
        axios.defaults.timeout=2000
        axios.defaults.responseType="json"
        const btn = document.querySelector("#btn")
        // console.log(btn)
        btn.onclick = () => {
            //请求的地址
            axios({
                url: "http://localhost:3000/students",
                // 请求参数
                method: 'GET',
                // params:({a:2,b:8}),
                // 配置请求体参数 json
                // data:{c:6,d:9},
                //配置请求体参数   url编码
                // data:'e=68&f=90',
                // 配置时间
                timeout: 2000, 
                headers:{school:'schoole'},
                 //默认的值
                responseType:'json'
            }).then(
                response => { console.log("请求成功", response.data) },
                error => { console.log("请求失败", error) }
            )
        }
    </script>
</body>
</html>

AJAX技术对比学习博客连接如下

2022年5月12号:Ajax第一课:_星辰镜的博客-CSDN博客

相关文章
|
2月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:
|
2月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
2月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
2月前
|
JSON 中间件 Java
【GoGin】(3)Gin的数据渲染和中间件的使用:数据渲染、返回JSON、浅.JSON()源码、中间件、Next()方法
我们在正常注册中间件时,会打断原有的运行流程,但是你可以在中间件函数内部添加Next()方法,这样可以让原有的运行流程继续执行,当原有的运行流程结束后再回来执行中间件内部的内容。​ c.Writer.WriteHeaderNow()还会写入文本流中。可以看到使用next后,正常执行流程中并没有获得到中间件设置的值。接口还提供了一个可以修改ContentType的方法。判断了传入的状态码是否符合正确的状态码,并返回。在内部封装时,只是标注了不同的render类型。再看一下其他返回的类型;
176 3
|
2月前
|
JSON Java Go
【GoGin】(2)数据解析和绑定:结构体分析,包括JSON解析、form解析、URL解析,区分绑定的Bind方法
bind或bindXXX函数(后文中我们统一都叫bind函数)的作用就是将,以方便后续业务逻辑的处理。
276 3
VUE2.0增删改查附编辑添加model(弹框)组件共用
因为也是纯粹的写写前端页面,所以数据方面用的是mock.js,真实的模拟请求。 这个项目用到的 技术栈: vue + webpack + vuex + axios 结构: build: webpack配置 config: 项目配置参数 common 共用的 components:组件...
2388 0
|
3月前
|
JavaScript
Vue中如何实现兄弟组件之间的通信
在Vue中,兄弟组件可通过父组件中转、事件总线、Vuex/Pinia或provide/inject实现通信。小型项目推荐父组件中转或事件总线,大型项目建议使用Pinia等状态管理工具,确保数据流清晰可控,避免内存泄漏。
323 2
|
2月前
|
缓存 JavaScript
vue中的keep-alive问题(2)
vue中的keep-alive问题(2)
301 137
|
6月前
|
人工智能 JavaScript 算法
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
Vue 中 key 属性的深入解析:改变 key 导致组件销毁与重建
811 0
|
6月前
|
JavaScript UED
用组件懒加载优化Vue应用性能
用组件懒加载优化Vue应用性能