Axios
Axios是一个基于promise网络请求库,作用于node.js和浏览器中,它是isomorphic的(即同一套代码可以运行在浏览器和node.js中)。
特性
从浏览器创建XMLHttpRequests
从node.js创建http请求
支持PromiseAPI
拦截请求和响应
转换请求和显影数据
取消请求
自动转换JSON数据
客户端支持防御XSRF
使用cdn:
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
用axios发起get请求不传递参数
axios.get('url地址+接口api')
.then(function(response){
console.log(response);//处理成功执行
})
.catch(funcition(error){
console.log(error); //处理失败执行
})
.then(function(){
//不管成功还是失败都会执行
})
用axios发起get请求并传递参数
axios.get('url地址+接口api',{
params:{
canshu:'canshu'
}
})
.then(function(response){
console.log(response);//处理成功执行
})
.catch(funcition(error){
console.log(error); //处理失败执行
})
.then(function(){
//不管成功还是失败都会执行
})
用axios发起post请求
axios.post('url地址+接口api',{
username:'usernam',
password:'upwd'
})
.then(function(response){
console.log(response);//处理成功执行
})
.catch(funcition(error){
console.log(error); //处理失败执行
})
.then(function(){
//不管成功还是失败都会执行
})