Axios浅了解
-
什么是Axios
Axios本质上还是对原生XMLHttpRequest的封装,可用于浏览器和nodejs的HTTP客户端,只不过它是基于Promise的,符合最新的ES规范。 -
具备以下特点:
- 在浏览器中创建XMLHttpRequest请求
- 在node.js中发送http请求
- 支持Promise API
- 拦截请求和响应
- 转换请求和响应数据
- 取消要求
- 自动转换JSON数据
- 客户端支持防止CSRF/XSRF(跨域请求伪造)
用Axios写GET方法
function axiosGet() {
axios({
method: 'get',
url: 'https://music.benwebsite.cloud/banner',
headers: {
'content-type': 'application/json'
} ,
params: {
type: 2
}
}).then(res => {
console.log('成功');
console.log(res);
}).then(err => {
console.log('err');
})
}
用Axios写POST方法
function axiosPost() {
axios({
method: 'post',
url: 'https://music.benwebsite.cloud/banner',
headers: {
'content-type': 'application/json'
} ,
params: {
type: 2
}
}).then(res => {
console.log('成功');
console.log(res);
}).then(err => {
console.log('err');
})
}
用Axios写PUT方法
function axiosPut() {
axios({
method: 'put',
url: 'https://music.benwebsite.cloud/banner',
headers: {
'content-type': 'application/json'
} ,
params: {
type: 2
}
}).then(res => {
console.log('成功');
console.log(res);
}).then(err => {
console.log('err');
})
}
用Axios写DELETE方法
function axiosDelete() {
axios({
method: 'delete',
url: 'https://music.benwebsite.cloud/banner',
headers: {
'content-type': 'application/json'
} ,
params: {
type: 2
}
}).then(res => {
console.log('成功');
console.log(res);
}).then(err => {
console.log('err');
})
}