国产探花免费观看_亚洲丰满少妇自慰呻吟_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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 故城县| 合水县| 丰都县| 商南县| 苍溪县| 潜江市| 克拉玛依市| 永济市| 青海省| 安徽省| 仙桃市| 阳高县| 梁河县| 靖安县| 固原市| 象州县| 丰顺县| 红桥区| 山东省| 辽宁省| 洞口县| 乌拉特后旗| 临邑县| 南溪县| 新蔡县| 舟山市| 嘉祥县| 杨浦区| 西丰县| 金沙县| 光山县| 司法| 资源县| 文水县| 八宿县| 普陀区| 沈阳市| 伊宁市| 和平区| 陆河县| 沧州市|