一、发送ajax请求
1.简介
vue本身不支持发送ajax请求,需使用vue-resource、axios等插件来实现
axios时一个基于Promise的HTTP请求客户端,用来发送请求,也就是vue2.0官方推荐的,同时不再对vue-resource进行更新和维护
参考:GitHub上搜索axios,查看API文档
2.使用axios发送ajax请求
2.1 安装axios并引入
npm install axios -s
2.2 基本用法
// 普通请求方式
axios({
method:'get',
url:'user.json'
}).then(function(resp){
console.log(resp.data)
}).catch(function(resp){
console.log('请求失败:'+resp)
})
//get请求方式
写法:axios.get(url).then(function(response){}).catch(function(error){});
axios.get("http://localhost:9527/?name='yang'&age=27").then(function(resp){
console.log(resp.data); // 输出: /?name=%22yang%22&age=%2227%22
}).catch(function(error){
console