刚开始看Vue,目前还停止在会使用API,做个笔记。
在使用axios进行网络请求时需要安装axios库,
- 使用npm 安装 命令行 npm install axios --save
- 使用cdn方式
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
如果使用的是npm方式,需要在<script></script>中进行引用
<!--导出模块-->
<script>
var axios = require('axios');
</script>
- Get请求
1.普通的get请求 通过&进行参数的拼接
//普通的网络请求
axios.get('http://127.0.0.1:8090/home/getlist?name=张&age=30').then(function(reponse){
console.log(reponse.data);
}).catch(function(error){
//异常处理
}).finally(function(){
});
2.Get请求 通过params进行参数传递
axios.get('http://127.0.0.1:8090/home/getlist',{
params:{
name:'张辉',
age:'30'
}
}).then(function(reponse){
console.log('param请求的参数'+reponse.data);
}).catch(function(error){
}).finally(function(){
});
- Post请求
axios.post('http://127.0.0.1:9091/user/register/test',{
name:'张辉',
age:'30'
}).then(function(reponse){
var resultJson = reponse.data;
console.log(JSON.stringify(resultJson)+'请求结果的响应');
}).catch(function(error){
}).finally(function(){
});