axios的使用

1.node.js 的安装

Node.js官网下载

1.Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。

在这里插入图片描述
根据自己系统选择下载,之后就是一直下一步就完成了

查看是否安装成功
在这里插入图片描述
这样就安装成功了

json server 的安装

JsonServer主要的作用就是搭建本地的数据接口,创建json文件,便于调试调用

执行代码:

npm install -g json-server

检查安装是否成功:
在这里插入图片描述

json server的使用

1.创建一个文件名为 db.json 的文件

{
          "posts": [
            { "id": 1, "title": "json-server", "author": "typicode" }
          ],
          "comments": [
            { "id": 1, "body": "some comment", "postId": 1 }
          ],
          "profile": { "name": "typicode" }
        }

2.启动 json server

json-server db.json

在这里插入图片描述
这样就是启动成功
3.关闭 json-server
在控制台里面输入ctrl+c再点击y就可以了

2.axios的使用

使用 jsDelivr CDN:

<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>

1.get请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
    <button>发送请求</button>
</div>
<script>
    //获取按钮
    const btn = document.querySelectorAll('button');
    //第一个按钮 按钮点击事件
    btn[0].onclick = function () {
        axios({
            //请求类型 默认请求
            method: 'get',
            url: ' http://localhost:3000/posts/'
        }).then(response => {
            console.log(response.data)
        })
    }
</script>
</body>
</html>

1、点击按钮后获得一个json 数据
在这里插入图片描述
2、再打开db.json
在这里插入图片描述
和这里面的数据是一样的

2.post请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
    <button>post请求</button>
</div>
<script>
    //获取按钮
    const btn = document.querySelectorAll('button');
    //第一个按钮 按钮点击事件
    btn[0].onclick = function () {
        axios({
            //请求类型 默认请求
            method: 'post',
            url: ' http://localhost:3000/posts/',
            //设置一个请求体
            data:{
                "title": "学习axios的第一天",
                "author": "若离"
            }
        }).then(response => {
            console.log(response.data)
        })
    }
</script>
</body>
</html>

1.点击按钮后查看控制台
在这里插入图片描述
2.查看db.json
在这里插入图片描述
里面是添加了一条新的数据的

3.put请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
    <button>put请求</button>
</div>
<script>
    //获取按钮
    const btn = document.querySelectorAll('button');
    //第一个按钮 按钮点击事件
    btn[0].onclick = function () {
        axios({
            //请求类型 默认请求
            method: 'put',
            url: ' http://localhost:3000/posts/2',
            //设置一个请求体
            data:{
                "title": "学习axios的第一天",
                "author": "蔡志强"
            }
        }).then(response => {
            console.log(response.data)
        })
    }
</script>
</body>
</html>

在这里插入图片描述
2.查看db.json数据
在这里插入图片描述
数据没有添加,但是内容已经更改

4.delete请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
    <button>delete请求</button>
</div>
<script>
    //获取按钮
    const btn = document.querySelectorAll('button');
    //第一个按钮 按钮点击事件
    btn[0].onclick = function () {
        axios({
            //请求类型 默认请求
            method: 'put',
            url: ' http://localhost:3000/posts/2',
        }).then(response => {
            console.log(response.data)
        })
    }
</script>
</body>
</html>

在这里插入图片描述
查看db.json
在这里插入图片描述

3.resquest 的使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>axios</title>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
    <button>delete请求</button>
</div>
<script>
    //获取按钮
    const btn = document.querySelectorAll('button');
    //第一个按钮 按钮点击事件
    btn[0].onclick = function () {
        axios.request({
            method:'get',
            url:' http://localhost:3000/posts'
        }).then(response=>{
            console.log(response.data);
        })
    }
</script>
</body>
</html>

请求的方法和上面也是一样的

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兔子猫猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值