[vue.js] use of axios

Posted by thimble on Wed, 12 Feb 2020 16:20:56 +0100

Article directory

axios Chinese document|axios Chinese net
http://www.axios-js.com/zh-cn/docs/

1, axios native JavaScript request

  • get method request random joke
  • post mode request registration interface

Request results to be printed on the console

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    </style>
</head>
<body>
        <input type="button" value="get" class="get">
        <input type="button" value="post" class="post">

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        // Random jokes
        // https://autumnfish.cn/api/joke/list?num=3
        document.querySelector(".get").onclick = function(){
            axios.get("https://autumnfish.cn/api/joke/list?num=3")
            .then(function(response){
                console.log(response);
            },function(err){
                console.log(err);
            })
        }
        document.querySelector(".post").onclick=function(){
            axios.post("https://autumnfish.cn/api/user/reg",{username:"dy"})
            .then(function(response){
                console.log(response);
            },function(err){
                console.log(err);
            })
        }
    </script>
</body>
</html>

2, Use of axios in vue

  • get mode requests random jokes, and the request results are displayed on the page
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <title>Document</title>
    </style>
</head>
<body>
    <div id="app">
        <input type="button" value="get" @click="getJoke">
        <p>{{joke}}</p>
    </div>
    <script>
        // Random jokes
        // https://autumnfish.cn/api/joke/list?num=3
        
        var app = new Vue({
            el:"#app",
            data:{
                joke:"joke"
            },
            methods:{
                getJoke:function(){
                    var that = this;
                    axios.get("https://autumnfish.cn/api/joke/list?num=1")
                    .then(function(response){
                        console.log(response.data);
                        console.log(response.data.jokes);
                         that.joke = response.data.jokes[0];
                    },function(err){
                        console.log(err);
                    })
                }
            }
        })
    </script>
</body>
</html>
  • get mode requests the weather interface, and the request result is printed on the console
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <title>Document</title>
    </style>
</head>
<body>
    <div id="app">
        <input type="text" v-model="city" @keyup.enter="searchWeather">
        <a href="#" @click="changeCity('Beijing')">Beijing</a>
        <a href="#" @click="changeCity('Xi'an')">Xi'an</a>
        <a href="#" @click="changeCity('Nanjing')">Nanjing</a>
        <a href="#" @click="changeCity('Changchun')">Changchun</a>
        <p>in:{{city}}</p>
    </div>
    <script>
        var app = new Vue({
            el:"#app",
            data:{
                city:'',
                weatherList:[]
            },
            methods:{
                searchWeather:function(){
                    axios.get("http://wthrcdn.etouch.cn/weather_mini?city="+this.city)
                    .then(function(response){
                        console.log(response);
                    },function(err){
                        console.log(err);
                    })
                },
                changeCity:function(city){
                    this.city=city;
                    this.searchWeather();
                }
            }
        })
    </script>
</body>
</html>

Published 125 original articles, praised 1001, visited 250000+
Private letter follow

Topics: axios Vue Javascript npm