vue請求后台接口 方法


ue不支持直接發送AJAX請求,需要使用vue-resource、axios等插件實現。

一.使用axios發送AJAX請求:

1.安裝axios並引入:

1)npm install axios -S (直接下載axios組件,下載完畢后axios.js就存放在node_modules\axios\dist中),首先在 main.js 中引入 axios:在此文件加入import axios from 'axios',如果在其它的組件中無法使用 axios 命令。可以將 axios 改寫為 Vue 的原型屬性:Vue.prototype.$http=axios,在 main.js 中添加了這兩行代碼之后,就能直接在組件的 methods 中使用 this.$http命令。

 2)網上直接下載axios.min.js文件,通過script src的方式進行文件的引入

2.發送請求:

1)get請求使用格式:

a:axios([options]) (這種格式直接將所有數據寫在options里,options其實是個字典)

b:axios.get(url[,options]);

<script>     
    new Vue({
             el:'#itany',
             data:{
                user:{
                     name:'alice',
                     age:19
                    },
                },
                methods:{
                    send(){
                        axios({//格式a
                            method:'get',
                            url:'http://www.baidu.com?name=tom&age=23'
                        }).then(function(resp){
                            console.log(resp.data);
                        }).catch(resp => {
                            console.log('請求失敗:'+resp.status+','+resp.statusText);
                        });
                    },
                    sendGet(){//格式b
                        axios.get('server.php',{
                            params:{
                                name:'alice',
                                age:19
                            }
                        })
                        .then(resp => {
                            console.log(resp.data);
                        }).catch(err => {             //
                            console.log('請求失敗:'+err.status+','+err.statusText);
                        });
                    },
                }
            });
    </script>

2)post請求格式:

a:axios.post(url,data,[options]);

new Vue({
                el:'#itany',
                data:{
                    user:{
                        name:'alice',
                        age:19
                    },
                },
                methods:{
                    sendPost(){
                        // axios.post('server.php',{
                        //         name:'alice',
                        //         age:19
                        // }) //該方式發送數據是一個Request Payload的數據格式,一般的數據格式是Form Data格式,所有發送不出去數據
                        // axios.post('server.php','name=alice&age=20&') //方式1通過字符串的方式發送數據
                        axios.post('server.php',this.user,{  //方式2通過transformRequest方法發送數據,本質還是將數據拼接成字符串
                            transformRequest:[
                                function(data){
                                    let params='';
                                    for(let index in data){
                                        params+=index+'='+data[index]+'&';
                                    }
                                    return params;
                                }
                            ]
                        })
                        .then(resp => {
                            console.log(resp.data);
                        }).catch(err => {
                            console.log('請求失敗:'+err.status+','+err.statusText);
                        });
                    },
                }
            });

3)發送跨域請求:

a:須知:axios本身並不支持發送跨域的請求,沒有提供相應的API,作者也暫沒計划在axios添加支持發送跨域請求,所以只能使用第三方庫

b:使用vue-resource發送跨域請求

c: 安裝vue-resource並引入    

   npm info vue-resource           #查看vue-resource 版本信息
      cnpm install vue-resource -S #等同於cnpm install vue-resource -save

d: 基本使用方法(使用this.$http發送請求) 

    this.$http.get(url, [options])

    this.$http.head(url, [options])

    this.$http.delete(url, [options])

    this.$http.jsonp(url, [options])

    this.$http.post(url, [body], [options])

    this.$http.put(url, [body], [options])

    this.$http.patch(url, [body], [options]) 

二.vue-resource發送請求:

1.安裝引入vue-resource方式:

1)npm install axios -S (直接下載axios組件,下載完畢后axios.js就存放在node_modules\axios\dist中),通過改路由的index.js引入:在index.js加入import VueResource from 'vue-resource'和Vue.use(VueResource)即可

2)網上直接下載axios.min.js文件,通過script src的方式進行文件的引入

2.post請求方式:

1)  this.$http({ method:'POST',  

                        url:'/a/b', //接口路徑 data:{'a':'123124'}, //參數 

                        headers: {"X-Requested-With": "XMLHttpRequest"}, 

                        }).then((res) => { if(res.body.code == "0") {
                        this.data= res.body.result;
                    } else {

                        this.warnMessage = "獲取班級失敗";
                        this.colorMessage = "red"
                    }
                }).catch(err => {
                    this.warnMessage = "訪問接口失敗";
                    this.colorMessage = "red"
                })

2)this.$http.post('../a/b/c', {}, {
                    header: {},
                    emulateJSON: true
                }).then((res) => {
                    if(res.body.code == "0") {
                        this.data= res.body.result;
                    } else {
                        this.warnMessage = "獲取班級失敗";
                        this.colorMessage = "red"
                    }
                }).catch(err => {
                    this.warnMessage = "訪問接口失敗";
                    this.colorMessage = "red"
                })

2.get請求方式同post,直接將上面的post改為get即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM