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

首頁 > 編程 > JavaScript > 正文

vue基礎之使用get、post、jsonp實現交互功能示例

2019-11-19 12:00:09
字體:
來源:轉載
供稿:網友

本文實例講述了vue基礎之使用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>

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.VeVB.COm/code/HtmlJsRun測試上述代碼運行效果。

希望本文所述對大家vue.js程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 正阳县| 西丰县| 宁海县| 阿尔山市| 洮南市| 金湖县| 高阳县| 桓仁| 镇坪县| 吴桥县| 天祝| 来凤县| 枞阳县| 崇左市| 湘潭市| 汨罗市| 武鸣县| 高雄县| 临猗县| 鄂尔多斯市| 海兴县| 衡南县| 枞阳县| 闻喜县| 垦利县| 舞阳县| 霍林郭勒市| 雅江县| 大丰市| 美姑县| 阿尔山市| 冕宁县| 柏乡县| 武城县| 五指山市| 巧家县| 六安市| 清镇市| 色达县| 南和县| 远安县|