什么是axios
- axios是一个获取后台数据的插件
- 地址https://www.npmjs.com/package/axios
- 使用 前提得安装node.js
- 页面直接引用
- 下载 选中文件夹 shift+右键 调出命令窗口 输入 npm i axios -g
获取数据:
路径为后台数据接口
在用axios获取后台数据时,
get function(){
var url = '路径'
axios.get(url,{params:参数}).then(function(储存后台数据的变量:A){ //then为成功后的回调
对象名.渲染页面的函数名(A.data) // data是在使用axios的时候,axios给数据添加了一个data来封装获得的数据,
}).catchcatch(function (用来储存错误信息的变量:error){ // 捕捉错误
alert(error) // 请求失败之后,执行这个函数
})
}
axios get 方法
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
aixos post方法
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
更多应用请看上面链接