Vue的學(xué)習(xí)筆記-Day2-03(用axios發(fā)送htttp請(qǐng)求)

用axios發(fā)送htttp請(qǐng)求

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="lib/vue-2.6.10.js"></script>
    <script src="lib/axios.min.js"></script>
</head>
<body>

<div id="app">
    <input type="button" value="get請(qǐng)求" @click="getInfo">
    <input type="button" value="post請(qǐng)求" @click="postInfo">
</div>

<script>
    //創(chuàng)建一個(gè)vue實(shí)例
    var vm = new Vue({
        el: '#app',
        data: {},
        methods: {
            getInfo() {//發(fā)起get請(qǐng)求
                //當(dāng)發(fā)起get請(qǐng)求后,通過(guò).then來(lái)設(shè)置成功的回調(diào)函數(shù)
                axios.get('https://v1.hitokoto.cn/').then(function (response) {
                    console.log(response);
                    //通過(guò)response拿到服務(wù)器的響應(yīng)內(nèi)容
                }).catch(function (error) {
                    console.log(error);
                });
                //為給定ID的user創(chuàng)建請(qǐng)求
                // axios.get('/user?ID=12345')
                //     .then(function (response) {
                //         console.log(response);
                //     })
                //     .catch(function (error) {
                //         console.log(error);
                //     });
                //上面的請(qǐng)求也可以這樣做
                // axios.get('/user', {
                //     params: {
                //         ID: 12345
                //     }
                // }).then(function (response) {
                //     console.log(response);
                // }).catch(function (error) {
                //     console.log(error);
                // });
            },
            postInfo() {//發(fā)起post請(qǐng)求
                //手動(dòng)發(fā)起的post請(qǐng)求,默認(rèn)沒(méi)有表單格式,所以,有的服務(wù)器處理不了
                //通過(guò)post方法的第三個(gè)參數(shù),設(shè)置提交的內(nèi)容類型為普通表單數(shù)據(jù)格式
                axios.post('https://v1.hitokoto.cn/', {
                    //這里可以添加參數(shù)
                }).then(response => {
                    console.log(response);
                }).catch(function (error) {
                    console.log(error);
                });
                //還可以通過(guò)配置參數(shù)進(jìn)行請(qǐng)求
                // 發(fā)送 POST 請(qǐng)求
                // axios({
                //     method: 'post',
                //     url: '/user/12345',
                //     data: {
                //         firstName: 'Fred',
                //         lastName: 'Flintstone'
                //     },
                //     responseType: 'json';//默認(rèn) json 可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
                // });

            }
        },
    })
</script>

</body>
</html>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容