Vue.js Ajax axiosGET 方法笔记
GET 方法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Vue 测试实例 - 菜鸟教程(runoob.com)</title>
<script src="https://cdn.staticfile.org/vue/2.4.2/vue.min.js"></script>
<script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
</head>
<body>
<div id="app">
<h1>网站列表</h1>
<div
v-for="site in info"
>
{{info}}
{{ site.name }}
</div>
</div>
<script type = "text/javascript">
new Vue({
el: '#app',
data () {
return {
info: null
}
},
mounted () {
axios
.get('https://www.runoob.com/try/ajax/json_demo.json')
.then(response => (this.info = response)) //现在把代码写成response
.catch(function (error) { // 请求失败处理
console.log(error);
});
}
})
</script>
</body>
可以看到传来的都是JSON 数据。
现在代码改成只取data
this.info = response.data
最后代码改成只取sites键:
GET把参数包含在URL中,POST通过request body传递参数。