欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

vue中如何發(fā)送ajax請(qǐng)求-創(chuàng)新互聯(lián)

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)vue中如何發(fā)送ajax請(qǐng)求,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

成都創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比沙河網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式沙河網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋沙河地區(qū)。費(fèi)用合理售后完善,十多年實(shí)體公司更值得信賴。

首頁(yè)安裝并引入axios

1、npm install axios -S        #直接下載axios組件,下載完畢后axios.js就存放在node_modules\axios\dist中

2、網(wǎng)上直接下載axios.min.js文件

3、通過(guò)script src的方式進(jìn)行文件的引入<script src="js/axios.min.js"></script>

axios基本使用方法

發(fā)送get請(qǐng)求

1、基本使用格式

格式1:axios([options]) #這種格式直接將所有數(shù)據(jù)寫在options里,options其實(shí)是個(gè)字典

格式2:axios.get(url[,options]);

2、傳參方式:

通過(guò)url傳參

通過(guò)params選項(xiàng)傳參

下面我們來(lái)看一個(gè)vue發(fā)送ajax get請(qǐng)求實(shí)例代碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>發(fā)送AJAX請(qǐng)求</title>
  <script src="js/vue.js"></script>
  <script src="js/axios.min.js"></script>
  <script>
    window.onload=function(){
      new Vue({
        el:'#itany',
        data:{
          user:{
            name:'alice',
            age:19
          },
        },
        methods:{
          send(){
            axios({
              method:'get',
              url:'http://www.baidu.com?name=tom&age=23'
            }).then(function(resp){
              console.log(resp.data);
            }).catch(resp => {
              console.log('請(qǐng)求失?。?#39;+resp.status+','+resp.statusText);
            });
          },
          sendGet(){
            axios.get('server.php',{
              params:{
                name:'alice',
                age:19
              }
            })
            .then(resp => {
              console.log(resp.data);
            }).catch(err => {       //
              console.log('請(qǐng)求失?。?#39;+err.status+','+err.statusText);
            });
          },
        }
      });
    }
  </script>
</head>
<body>
  <div id="itany">
    <button @click="send">發(fā)送AJAX請(qǐng)求</button>

    <button @click="sendGet">GET方式發(fā)送AJAX請(qǐng)求</button>

  </div>
</body>
</html>

發(fā)送post請(qǐng)求(push,delete的非get方式的請(qǐng)求都一樣)

1、基本使用格式

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

2、傳參方式

1、自己拼接為鍵值對(duì)

2、使用transformRequest,在請(qǐng)求發(fā)送前將請(qǐng)求數(shù)據(jù)進(jìn)行轉(zhuǎn)換

3、如果使用模塊化開(kāi)發(fā),可以使用qs模塊進(jìn)行轉(zhuǎn)換

4、注釋:axios默認(rèn)發(fā)送post數(shù)據(jù)時(shí),數(shù)據(jù)格式是Request Payload,并非我們常用的Form Data格式,所以參數(shù)必須要以鍵值對(duì)形式傳遞,不能以json形式傳參

下面看是一個(gè)vue發(fā)送ajax post請(qǐng)求實(shí)例代碼

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>發(fā)送AJAX請(qǐng)求</title>
  <script src="js/vue.js"></script>
  <script src="js/axios.min.js"></script>
  <script>
    window.onload=function(){
      new Vue({
        el:'#itany',
        data:{
          user:{
            name:'alice',
            age:19
          },
        },
        methods:{
          sendPost(){
            // axios.post('server.php',{
            //     name:'alice',
            //     age:19
            // }) //該方式發(fā)送數(shù)據(jù)是一個(gè)Request Payload的數(shù)據(jù)格式,一般的數(shù)據(jù)格式是Form Data格式,所有發(fā)送不出去數(shù)據(jù)
            // axios.post('server.php','name=alice&age=20&') //方式1通過(guò)字符串的方式發(fā)送數(shù)據(jù)
            axios.post('server.php',this.user,{ //方式2通過(guò)transformRequest方法發(fā)送數(shù)據(jù),本質(zhì)還是將數(shù)據(jù)拼接成字符串
              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('請(qǐng)求失敗:'+err.status+','+err.statusText);
            });
          },
        }
      });
    }
  </script>
</head>
<body>
  <div id="itany">
    <button @click="send">發(fā)送AJAX請(qǐng)求</button>

    <button @click="sendGet">GET方式發(fā)送AJAX請(qǐng)求</button>

  </div>
</body>
</html>

上面我們所介紹的vue發(fā)送ajax請(qǐng)求都是在同一域名下進(jìn)行的也就是同域或者說(shuō)是同源

那么vue如何發(fā)送跨域的ajax請(qǐng)求呢?

vue發(fā)送跨域ajax請(qǐng)求

1、須知:axios本身并不支持發(fā)送跨域的請(qǐng)求,沒(méi)有提供相應(yīng)的API,作者也暫沒(méi)計(jì)劃在axios添加支持發(fā)送跨域請(qǐng)求,所以只能使用第三方庫(kù)

2、使用vue-resource發(fā)送跨域請(qǐng)求

3、 安裝vue-resource并引入 

npm info vue-resource           #查看vue-resource 版本信息

cnpm install vue-resource -S #等同于cnpm install vue-resource -save

4、基本使用方法(使用this.$http發(fā)送請(qǐng)求)

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])

下面再來(lái)看兩個(gè)實(shí)例

向360搜索發(fā)送數(shù)據(jù)

<!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">
  <title>發(fā)送AJAX請(qǐng)求</title>
  <script src="js/vue.js"></script>
  <script src="js/axios.js"></script>
  <script src="js/vue-resource.js"></script>
</head>

<body>
<div id="itany">
  <a>{{name}}</a>

  <button v-on:click="send">sendJSONP</button>

</div>
</body>
<script>
  new Vue({
    el: '#itany',
    data:{
      name: 'alice',
      age: 19
          },
    methods:{
      send:function(){
//        https://sug.so.#/suggest?callback=suggest_so&encodein=utf-8&encodeout=utf-8&format=json&fields=word&word=a
        this.$http.jsonp('https://sug.so.#/suggest',
          {params:{
            word:'a'
          }}
        ).then(function (resp) {
          console.log(resp.data)
        })

      }
    }
  })
</script>
</html>

vue發(fā)送跨域ajax請(qǐng)求帶jsonp參數(shù)

<!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">
  <title>發(fā)送AJAX請(qǐng)求</title>
  <script src="js/vue.js"></script>
  <script src="js/axios.js"></script>
  <script src="js/vue-resource.js"></script>
</head>
<body>
<div id="itany">
  <button v-on:click="send">向百度搜索發(fā)送JSONP請(qǐng)求</button>
</div>
</body>
<script>
  new Vue({
    el:'#itany',
    data:{
      name:'za'
    },
    methods:{
      send:function () {
        this.$http.jsonp('https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
          {params:{wd:'a'},
            jsonp:'cb', //百度使用的jsonp參數(shù)名為cb,所以需要修改,默認(rèn)使用的是callbakc參數(shù)就不用修改
          }).then(function (resp) {
          console.log(resp.data)
        }).catch(function (err) {
          console.log(err)
        })
      }
    }


  })
</script>
</html>

Vue作為一個(gè)沒(méi)有入侵性的框架并不限制你使用ajax框架

使用了Vue后,ajax部分你可以做如下選擇:

1.使用JS原生XHR接口

2.引入JQuery或者Zepto 使用$.ajax();

3.Vue的github上提供了vue-resource插件 :

4.使用 fetch.js

5.自己封裝一個(gè)ajax庫(kù)

上述就是小編為大家分享的vue中如何發(fā)送ajax請(qǐng)求了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

文章標(biāo)題:vue中如何發(fā)送ajax請(qǐng)求-創(chuàng)新互聯(lián)
本文路徑:http://aaarwkj.com/article14/coepge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器標(biāo)簽優(yōu)化、建站公司做網(wǎng)站、微信小程序、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁(yè)設(shè)計(jì)公司
欧美日韩激情在线一区| 亚洲一本一道久久香蕉| 亚洲精品一品区二品区三区| 亚洲午夜福利理论片在线| 欧美二区三区精品在线| 国产高清自拍视频在线一区| 久久婷婷国产综合色啪| 日韩毛片免费看美日韩毛片| 欧美日韩在线视频第三区| 日韩精品毛片精品一区到三区| 深夜av一区二区三区| 中文字幕久久熟女蜜桃| 成人欧美黑人一区二区| 琪琪精品免费一区二区三区| 亚洲精品a在线观看av| 色哟哟网站在线观看入口| 日韩欧美国产亚洲在线| 亚洲成av人一区二区三区| 亚洲新大香蕉视频在线播放| 日韩欧美一区二区在线播放| 亚洲av一区二区三区色多多| 国产亚洲一区二区三区成人| 香港精品国产三级国产av| 中文字幕人妻出轨一区二区| 国产自产一区二区三区精品| 最新中文字幕成人在线观看| 尤物视频官网在线观看| 中文字幕亚洲精品视频| 五月天亚洲激情综合av| 日本av高清视频在线观看| 国产欧美日韩精品av| 在线看片国产精品自拍| 蜜桃成人一区二区三区| 亚洲国产高清第一第二区| 亚洲国产日韩在线精品| 久碰精品少妇中文字幕av| 夜福利国产视频大屁股| 日日夜夜久久国产精品| 69久久精品费精品国产| 白白在线观看网站看看亚洲| 成人在线观看av毛片|