vue.js 本身不支持发送ajax,需要 依赖(vue1.0)vue-resource、(vue2.0)axios jquery(ajax)
vue2.0 不推荐 vue-resource
axios是一个 基于Promise的http请求客户端
用来来发送请求
npm安装
$ npm install axios
bower安装
$ bower install axios
通过cdn
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
get.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="../js/vue-2.6.10.js"></script>
<!-- <script src="../js/axios.js"></script> -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<title>发送get请求</title>
<script type="text/javascript">
window.onload=function(){
new Vue({
el:'#app',
data:{
users:{
name:'',
age:''
}
},
methods:{
//axios.get 的发送参数有两种,两个ajax请求函数都可实现
sendGetByStr(){
//1.get通过直接发送字符串拼接
axios.get('get.php?name=${this.users.name}&age=${this.user.age}')
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});