当前阅读教程:Vue.JS入门教程 > GET 方法 | |
阅读(22555525) 收藏 赞(5685) 分享 | |
上一篇: 安装方法 | 下一篇: POST 方法 |
我们可以简单的读取 JSON 数据: GET 实例 new Vue({ el: '#app', data () { return { info: null } }, mounted () { axios .get('https://域名/json_demo.json') .then(response => (this.info = response)) .catch(function (error) { // 请求失败处理 console.log(error); }); } }) 使用 response.data 读取 JSON 数据:
GET 实例 <div id="app"> <h1>网站列表</h1> <div v-for="site in info" > {{ site.name }} </div> </div> <script type = "text/javascript"> new Vue({ el: '#app', data () { return { info: null } }, mounted () { axios .get('https://域名/json_demo.json') .then(response => (this.info = response.data.sites)) .catch(function (error) { // 请求失败处理 console.log(error); }); } }) </script>
GET 方法传递参数格式如下:
传递参数说明 // 直接在 URL 上添加参数 ID=12345 axios.get('/user?ID=12345') .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
// 也可以通过 params 设置参数: axios.get('/user', { params: { ID: 12345 } }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); }); |
|
上一篇: 安装方法 | 下一篇: POST 方法 |