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博客

目录
打赏
0
0
0
0
6
分享
相关文章
淘宝商品详情API的调用流程(python请求示例以及json数据示例返回参考)
JSON数据示例:需要提供一个结构化的示例,展示商品详情可能包含的字段,如商品标题、价格、库存、描述、图片链接、卖家信息等。考虑到稳定性,示例应基于淘宝开放平台的标准响应格式。
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——封装统一返回的数据结构
本文介绍了在Spring Boot中封装统一返回的数据结构的方法。通过定义一个泛型类`JsonResult&lt;T&gt;`,包含数据、状态码和提示信息三个属性,满足不同场景下的JSON返回需求。例如,无数据返回时可设置默认状态码&quot;0&quot;和消息&quot;操作成功!&quot;,有数据返回时也可自定义状态码和消息。同时,文章展示了如何在Controller中使用该结构,通过具体示例(如用户信息、列表和Map)说明其灵活性与便捷性。最后总结了Spring Boot中JSON数据返回的配置与实际项目中的应用技巧。
88 0
|
20天前
|
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——使用 fastJson 处理 null
本文介绍如何使用 fastJson 处理 null 值。与 Jackson 不同,fastJson 需要通过继承 `WebMvcConfigurationSupport` 类并覆盖 `configureMessageConverters` 方法来配置 null 值的处理方式。例如,可将 String 类型的 null 转为 &quot;&quot;,Number 类型的 null 转为 0,避免循环引用等。代码示例展示了具体实现步骤,包括引入相关依赖、设置序列化特性及解决中文乱码问题。
47 0
|
20天前
|
微服务——SpringBoot使用归纳——Spring Boot返回Json数据及数据封装——Spring Boot 默认对Json的处理
本文介绍了在Spring Boot中返回Json数据的方法及数据封装技巧。通过使用`@RestController`注解,可以轻松实现接口返回Json格式的数据,默认使用的Json解析框架是Jackson。文章详细讲解了如何处理不同数据类型(如类对象、List、Map)的Json转换,并提供了自定义配置以应对null值问题。此外,还对比了Jackson与阿里巴巴FastJson的特点,以及如何在项目中引入和配置FastJson,解决null值转换和中文乱码等问题。
45 0
淘宝商品详情API接口概述与JSON数据示例
淘宝商品详情API是淘宝开放平台提供的核心接口之一,为开发者提供了获取商品深度信息的能力。以下是技术细节和示例:
【Vue面试题二十五】、你了解axios的原理吗?有看过它的源码吗?
这篇文章主要讨论了axios的使用、原理以及源码分析。 文章中首先回顾了axios的基本用法,包括发送请求、请求拦截器和响应拦截器的使用,以及如何取消请求。接着,作者实现了一个简易版的axios,包括构造函数、请求方法、拦截器的实现等。最后,文章对axios的源码进行了分析,包括目录结构、核心文件axios.js的内容,以及axios实例化过程中的配置合并、拦截器的使用等。
【Vue面试题二十五】、你了解axios的原理吗?有看过它的源码吗?
【Vue面试题二十七】、你了解axios的原理吗?有看过它的源码吗?
文章讨论了Vue项目目录结构的设计原则和实践,强调了项目结构清晰的重要性,提出了包括语义一致性、单一入口/出口、就近原则、公共文件的绝对路径引用等原则,并展示了单页面和多页面Vue项目的目录结构示例。

热门文章

最新文章