美文网首页
3 axios({})基本使用

3 axios({})基本使用

作者: 无知者3 | 来源:发表于2022-04-02 12:42 被阅读0次

1 get请求

            axios({
                method: 'GET',
                url: 'http://localhost:3000/posts',
            }).then(resp => {
                console.log(resp)
            })
        })
image.png

2 post请求

            axios({
                method: 'POST',
                url: 'http://localhost:3000/posts',
                data: {
                    "title": "今日重大新闻",
                    "author": "佚名"
                }
            })

发送post请求后数据增加:


image.png

3 修改

            axios({
                method: "PUT",
                url: 'http://localhost:3000/posts/3',
                data: {
                    "title": "今日小新闻",
                    "author": "佚名"
                }
            }).then(resp => {
                console.log(resp)
            })
image.png

4 删除

            axios({
                method: "DELETE",
                url: 'http://localhost:3000/posts/3'
            }).then(resp => {
                console.log(resp)
            })
image.png

附录:

index.html

<!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>Document</title>
    <script src="../js/axios.min.js"></script>
</head>

<body>
    <button class="get">get请求</button>
    <button class="post">post请求</button>
    <button class="put">put请求</button>
    <button class="delete">delete请求</button>
    <script>
        //get请求
        const btn_get = document.querySelector('.get')
        btn_get.addEventListener('click', e => {
            axios({
                method: 'GET',
                url: 'http://localhost:3000/posts',
            }).then(resp => {
                console.log(resp)
            })
        })
        const btn_post = document.querySelector('.post')
        btn_post.addEventListener('click', e => {
            axios({
                method: 'POST',
                url: 'http://localhost:3000/posts',
                data: {
                    "title": "今日重大新闻",
                    "author": "佚名"
                }
            }).then(resp => {
                console.log(resp)
            })
        })
        const btn_put = document.querySelector('.put')
        btn_put.addEventListener('click', e => {
            axios({
                method: "PUT",
                url: 'http://localhost:3000/posts/3',
                data: {
                    "title": "今日小新闻",
                    "author": "佚名"
                }
            }).then(resp => {
                console.log(resp)
            })
        })
        const btn_delete = document.querySelector('.delete')
        btn_delete.addEventListener('click', e => {
            axios({
                method: "DELETE",
                url: 'http://localhost:3000/posts/3'
            }).then(resp => {
                console.log(resp)
            })
        })
    </script>
</body>

</html>

相关文章

  • 3 axios({})基本使用

    1 get请求 2 post请求 发送post请求后数据增加: 3 修改 4 删除 附录: index.html

  • 六、Vue.js

    一、发送AJAX请求 1.简介 2,使用axios发送ajax请求 2.1安装axios并引入 2.1基本用法 3...

  • axios基本使用

    1.axios({config}) 默认是get请求 带有参数的url get参数可以写在params配置里,po...

  • 2020-01-11 axios的基本使用

    axios的基本使用 安装axios,因为运行时也要用到所以是--savenpm install axios --...

  • Vue3.0 项目中遇到的问题(十二)

    一. axios的基本使用 二. axios的初步封装 (1) 详细流程图: 三. axios 对有的实例...

  • axios

    axios axios 是一个专注于网络请求的库! axios 的基本使用:繁 发送get请求: 发送post请求...

  • Vue使用Axios小结 模拟form表单提交

    1、安装axios 2、引入axios 3、定义request.js 4、使用

  • axios配置相关

    参考文档 Axios 请求配置参数详解 axios 全攻略之基本介绍与使用(GET 与 POST)

  • axios的基本使用

    一、安装 cd 项目的目录 npm install --save --save-exact axios vue-a...

  • Axios的基本使用

    axios使用 作为全局对象来使用,不像vue-resource挂在在Vue实例上。 引入 npm install...

网友评论

      本文标题:3 axios({})基本使用

      本文链接:https://www.haomeiwen.com/subject/xxsxsrtx.html