axios(config): 通用/最本质的发任意类型请求的方式
axios(url[, config]): 可以只指定 url 发 get 请求
axios.request(config): 等同于 axios(config)
axios.get(url[, config]): 发 get 请求
axios.delete(url[, config]): 发 delete 请求
axios.post(url[, data, config]): 发 post 请求
axios.put(url[, data, config]): 发 put 请求
axios.defaults.xxx: 请求的默认全局配置
axios.interceptors.request.use(): 添加请求拦截器
axios.interceptors.response.use(): 添加响应拦截器
axios.create([config]): 创建一个新的 axios(它没有下面的功能)
axios.Cancel(): 用于创建取消请求的错误对象
axios.CancelToken(): 用于创建取消请求的 token 对象
axios.isCancel(): 是否是一个取消请求的错误
axios.all(promises): 用于批量执行多个异步请求
axios.spread(): 用来指定接收所有成功数据的回调函数的方法
//获取按钮
let btns = document.getElementsByTagName('button');
//绑定事件
btns[0].addEventListener('click',function() {
//发送GET AJAX请求
axios({
method:'GET',
url:'http://localhost:3000/posts/1'
}).then(response=>{
console.log(response);
})
})
//添加一篇新的文章
btns[1].addEventListener('click',function() {
//发送POST AJAX请求
axios({
method:'POST',
url:'http://localhost:3000/posts',
//设置请求体
data:{
title:"马上快开学了!",
author:"蓝朽"
}
}).then(response=>{
console.log(response);
})
})
//更新数据
btns[2].addEventListener('click',function() {
//发送PUT AJAX请求
axios({
method:'PUT',
url:'http://localhost:3000/posts/3',
//设置请求体
data:{
title:"马上快开学了!",
author:"王五"
}
}).then(response=>{
console.log(response);
})
})
//删除文章
btns[3].addEventListener('click',function() {
//发送delete AJAX请求
axios({
method:'DELETE',
url:'http://localhost:3000/posts/3',
}).then(response=>{
console.log(response);
})
})
btns[0].addEventListener('click',function(){
axios.request({
method:"get",
url:"http://localhost:3000/comments"
}).then(response=>{
console.log(response)
})
})
//获取按钮
const btns = document.querySelectorAll('button');
//默认配置
axios.defaults.method = 'GET';//设置默认的请求类型为 GET
axios.defaults.baseURL = 'http://localhost:3000';//设置基础 URL
axios.defaults.params = {id:100};
axios.defaults.timeout = 3000;//
btns[0].onclick = function(){
axios({
url: '/posts'
}).then(response => {
console.log(response);
})
}
//获取按钮
const btns = document.querySelectorAll('button');
//创建实例对象
let aServer = axios.create({
baseURL:'http://localhost:3000',
timeout: 3000
})
//第二个服务器的配置
let bServer = axios.create({
baseURL:'http://localhost:3000',
timeout: 3000
})
//这里aServer和axios对象的功能几乎一样
aServer({
url:'/comments'
}).then(res=>{
console.log(res)
})
//发送get请求
aServer.get('/comments').then(res=>{console.log(res.data)})
// Promise
// 设置请求拦截器 config 配置对象
axios.interceptors.request.use(function (config) {
console.log("请求拦截器 成功 1号");
//修改config中的参数
config.params={a:100}
return config;
}, function (error) {
console.log("请求拦截器 失败 1号");
return Promise.reject(error);
});
axios.interceptors.request.use(function (config) {
console.log("请求拦截器 成功 2号");
//修改config中的参数
config.timeout=2000
return config;
}, function (error) {
console.log("请求拦截器 失败 2号");
return Promise.reject(error);
});
// 设置响应拦截器
axios.interceptors.response.use(function (response) {
console.log("响应拦截器 成功 -1号");
return response.data;
// return response;
}, function (error) {
console.log("响应拦截器 失败 -1号");
return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
console.log("响应拦截器 成功 -2号");
return response;
}, function (error) {
console.log("响应拦截器 失败 -2号");
return Promise.reject(error);
});
//发送请求
axios({
method: 'GET',
url: 'http://localhost:3000/posts'
}).then(response => {
console.log('自定义回调处理成功的结果');
console.log(response);
}).catch(e=>{
console.log("自定义失败回调");
});
//获取按钮
const btns = document.querySelectorAll('button');
//2.声明全局变量
let cancel = null;
//发送请求
btns[0].onclick = function(){
//检测上一次请求是否完成
if(cancel !==null){
//取消上一次的请求
cancel();
}
axios({
method: 'GET',
url: 'http://localhost:3000/posts',
//1. 添加配置对象的属性
cancelToken: new axios.CancelToken(function(c){
//3. 将 c 的值赋值给 cancel
cancel = c;
})
}).then(response => {
console.log(response);
//将 cancel 的值初始化
cancel = null;
})
}
//绑定第二个事件取消请求
btns[1].onclick = function(){
cancel();
}