標籤:oct lan tps title images doctype .com 教程 str
vue教程1-08 互動 get、post、jsonp
一、如果vue想做互動,引入: vue-resouce
二、get方式
1、get擷取一個普通文本資料:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> </style> <script src="vue.js"></script> <script src="vue-resource.js"></script> <script> window.onload=function(){ new Vue({ el:‘body‘, data:{ }, methods:{ get:function(){ this.$http.get(‘a.txt‘).then(function(res){ alert(res.status);//成功 alert(res.data); },function(res){ alert(res.status);//失敗返回 alert(res.data); }); } } }); }; </script></head><body> <input type="button" value="按鈕" @click="get()"></body></html>
2、get給服務發送資料:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> </style> <script src="vue.js"></script> <script src="vue-resource.js"></script> <script> window.onload=function(){ new Vue({ el:‘body‘, data:{ }, methods:{ get:function(){ this.$http.get(‘get.php‘,{ a:1, b:2 }).then(function(res){ alert(res.data); },function(res){ alert(res.status); }); } } }); }; </script></head><body> <input type="button" value="按鈕" @click="get()"></body></html>
二、post方式
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> </style> <script src="vue.js"></script> <script src="vue-resource.js"></script> <script> window.onload=function(){ new Vue({ el:‘body‘, data:{ }, methods:{ get:function(){ this.$http.post(‘post.php‘,{ a:1, b:20 },{ emulateJSON:true }).then(function(res){ alert(res.data); },function(res){ alert(res.status); }); } } }); }; </script></head><body> <input type="button" value="按鈕" @click="get()"></body></html>
四、jsonp方式
擷取百度介面
查看響應資料
jsonp請求百度介面
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> </style> <script src="vue.js"></script> <script src="vue-resource.js"></script> <script> window.onload=function(){ new Vue({ el:‘body‘, data:{ }, methods:{ get:function(){ this.$http.jsonp(‘https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su‘,{ wd:‘a‘ },{ jsonp:‘cb‘//回呼函數名稱 }).then(function(res){ alert(res.data.s); },function(res){ alert(res.status); }); } } }); }; </script></head><body> <input type="button" value="按鈕" @click="get()"></body></html>
jsonp請求360介面
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title></title> <style> </style> <script src="vue.js"></script> <script src="vue-resource.js"></script> <script> window.onload=function(){ new Vue({ el:‘body‘, data:{ }, methods:{ get:function(){ this.$http.jsonp(‘https://sug.so.360.cn/suggest‘,{ word:‘a‘ }).then(function(res){ alert(res.data.s); },function(res){ alert(res.status); }); } } }); }; </script></head><body> <input type="button" value="按鈕" @click="get()"></body></html>
vue教程1-08 互動 get、post、jsonp