国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > AJAX > 正文

詳解vue與后端數(shù)據(jù)交互(ajax):vue-resource

2024-09-01 08:33:51
字體:
供稿:網(wǎng)友

本人對vue與后端數(shù)據(jù)交互不是很懂,搜索了很多關(guān)于vue與后端數(shù)據(jù)交互介紹,下面我來記錄一下,有需要了解的朋友可參考。希望此文章對各位有所幫助。

必須引入一個(gè)庫:vue-resource

1.獲取普通文本數(shù)據(jù)

比如:a.txt:

welcomet to vue!!!

<!DOCTYPE html><html><head>  <title></title>  <meta charset="utf-8">  <script src="http://unpkg.com/vue/dist/vue.js"></script>  <script src="https://cdn.jsdelivr.net/vue.resource/1.0.3/vue-resource.min.js"></script>  <script type="text/javascript">    window.onload = function(){      var vm = new Vue({        el:'#box',        data:{          msg:'Hello World!',        },        methods:{          get:function(){            //發(fā)送get請求            this.$http.get('a.txt').then(function(res){              alert(res.body);              },function(){              console.log('請求失敗處理');            });          }        }      });    }  </script></head><body>   <div id="box">    <input type="button" @click="get()" value="按鈕">  </div></body></html>

上面代碼實(shí)現(xiàn)了,點(diǎn)擊按鈕,就發(fā)送get請求,然后把拿到的數(shù)據(jù)alert出來。

vue與后端數(shù)據(jù)交互,vuejs與后端交互,vue,前后端交互

2.get發(fā)送數(shù)據(jù)給后端

假設(shè)后端地址是get.PHP,代碼如下:

<?php$a = $_GET['a'];$b = $_GET['b'];$c = $a + $b;die($c);
this.$http.get('get.php',{a:1,b:2}).then(function(res){ alert(res.body);   },function(res){         console.log(res.status);        });

this.$http.get('get.php',jsonData) 第二個(gè)參數(shù)就是傳到后端的數(shù)據(jù)。

3.post請求

post發(fā)送數(shù)據(jù)到后端,還需要第三個(gè)參數(shù):{emulateJSON:true}

            this.$http.post('post.php',{a:1,b:2},{emulateJSON:true}).then(function(res){              alert(res.body);              },function(res){              console.log(res.status);            });

4.jsonp

這是360搜索jsonp的接口: https://sug.so.360.cn/suggest?callback=suggest_so&word=a

vue與后端數(shù)據(jù)交互,vuejs與后端交互,vue,前后端交互

我們看vue-resource如何使用jsonp

            this.$http.jsonp('https://sug.so.360.cn/suggest',{word:'a'},{jsonp:'callback'}).then(function(res){              console.log(res.data);            },function(res){              console.log(res.status);            });

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 新乡市| 共和县| 西畴县| 瑞安市| 赞皇县| 长白| 高密市| 体育| 临安市| 垦利县| 康马县| 措美县| 九龙城区| 呼伦贝尔市| 铁岭县| 寻甸| 闵行区| 顺平县| 兴义市| 海门市| 阳江市| 南木林县| 肇庆市| 张家川| 新乡市| 晋城| 遂平县| 白山市| 大关县| 新巴尔虎左旗| 金门县| 高要市| 瑞昌市| 连州市| 巨野县| 武义县| 大厂| 宁陵县| 康平县| 无为县| 仲巴县|