Vue架構axios請求(類似於ajax請求)

來源:互聯網
上載者:User

標籤:alert   baidu   ade   password   vue.js   cli   查看   win   charset   

Vue架構axios get請求(類似於ajax請求)

首先介紹下,這個axios請求最明顯的地方,通過這個請求進行提交的時候頁面不會重新整理

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script src="vue.js"></script>    <script src="axios.js"></script></head><body><div id="myapp">    <input type="button" v-on:click="showlist" value="點我">    <ul>        <li v-for="item in stulist">            代碼:{{ item.cityCode}}            城市:{{ item.cityName}}        </li>    </ul></div></body><script>    new Vue({        el:‘#myapp‘,        data:{            stulist:[]        },        methods:{            showlist:function () {                url="./hotcity.json";    這是一個自訂的json資料檔案                var self = this;                axios.get(url)                    .then(function (response) {                        self.stulist = response.data.data.hotCity;                        console.log(response)                    .catch(function (err) {                      })                 })            }        }    })</script></html>
Vue架構axios post請求(類似於ajax請求)

這個查看資料使用get請求,但是添加資料的時候如果使用get請求的話,需要添加的資料就會暴露在url中。所以使用axios中的post請求將資料存放在請求體中

這樣使用者的資料就會很安全。

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title>    <script src="vue.js"></script>    <script src="axios.js"></script></head><body>    <div id="myapp">        <input type="text" value="username" v-model="uname">        <input type="text" value="password" v-model="passw">        <input type="button" value="提交" v-on:click="login">    </div>    <script>        new Vue({            el:‘#myapp‘,            data: {                uname:‘‘,                passw:‘‘            },            methods:{                login:function () {                    alert(222);                    url = "hotcity.json";                    axios.post(url,{                        name:this.uname,                        password:this.passw                    },{                        "headers":{"Content-Type":"application/x-www-form-urlencoded"}                    }).then(function (response) {                        console.log(response)                        if (response.code == 1){                            window.location.href = "http://www.baidu.com"                        }                    }).catch(function (error) {                                            })                }            }        })    </script></body></html>

  

Vue架構axios請求(類似於ajax請求)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.