1.axios的基本使用
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
</head
>
<body
>
<script src
="./js/axios.js"></script
>
<script
>
axios
.get('http://localhost:3000/adata').then(function(ret
) {
console
.log(ret
);
console
.log(ret
.data
);
})
</script
>
</body
>
</html
>
2.axios get请求传递参数
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
</head
>
<body
>
<script src
="./js/axios.js"></script
>
<script
>
axios
.delete('http://localhost:3000/axios', {
params
: {
id
: 111
}
}).then(function(ret
) {
console
.log(ret
);
console
.log(ret
.data
);
})
</script
>
</body
>
</html
>
3.post请求传递参数
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
</head
>
<body
>
<script src
="./js/axios.js"></script
>
<script
>
axios
.put('http://localhost:3000/axios/123', {
uname
: 'lisi',
pwd
: 123
})
.then(res
=> {
console
.log(res
)
console
.log(res
.data
);
})
.catch(err
=> {
console
.error(err
);
})
</script
>
</body
>
</html
>
4.axios的响应结果与全局配置
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
</head
>
<body
>
<script src
="./js/axios.js"></script
>
<script
>
axios
.defaults
.baseURL
= "http://localhost:3000/";
axios
.defaults
.headers
["mytoken"] = "hello";
axios
.get('axios-json')
.then(res
=> {
console
.log(res
)
})
.catch(err
=> {
console
.error(err
);
})
</script
>
</body
>
</html
>
5.axios的拦截器
<!DOCTYPE html
>
<html lang
="en">
<head
>
<meta charset
="UTF-8">
<meta name
="viewport" content
="width=device-width, initial-scale=1.0">
<title
>Document
</title
>
</head
>
<body
>
<script src
="./js/axios.js"></script
>
<script
>
axios
.interceptors
.request
.use(config
=> {
console
.log(config
.url
);
config
.headers
.mytoken
= "nihao"
return config
;
}, error
=> {
return Promise
.reject(error
);
});
axios
.interceptors
.response
.use(response
=> {
console
.log(response
);
var data
= response
.data
return data
;
}, error
=> {
return Promise
.reject(error
);
});
axios
.get('http://localhost:3000/adata')
.then(res
=> {
console
.log(res
);
})
.catch(err
=> {
console
.error(err
);
});
</script
>
</body
>
</html
>
6.axios的全局配置
7.创建axios实例
8.axios封装
9axios拦截器
转载请注明原文地址: https://lol.8miu.com/read-12835.html