# axios **Repository Path**: xorangex/axios ## Basic Information - **Project Name**: axios - **Description**: axios源码解析 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-04-25 - **Last Updated**: 2024-04-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: Ajax, Axios ## README ### 一、json-server 地址:https://github.com/typicode/json-server 1、安装依赖:`npm install json-server` 2、创建和配置 db.json 文件 3、启动 `json-server db.json` ### 二、axios 的使用 地址:https://github.com/axios/axios 1、基本使用 ```js btn.onclick = function(){ axios({ url: 'http://localhost:3000/posts', method: 'get' }).then(res => { console.log(res) }); } ``` 2、其他使用 ```js btn.onclick = function(){ axios.get(url: 'http://localhost:3000/posts',).then(res => { console.log(res) }); } ``` 3、axios的默认配置 ```js // 默认配置 axios.defaults.method = 'get'; // 设置默认的请求类型为 get axios.defaults.baseURL = 'http://localhost:3000'; // 设置默认的请求地址 axios.defaults.params = { id: 100 }; // 设置默认的请求参数 axios.defaults.timeout = 5000; // 设置默认的超时时间 // 发送请求 btns[0].onclick = function(){ axios({ url: '/posts' }).then( response => { console.log(response); }); } ``` 4、创建实例对象及使用 ```js // 创建实例对象 /getJoke const duanzi = axios.create({ baseURL: 'https://api.apiopen.top/', timeout: 5000 }); duanzi.get('/getJoke').then(res =>{ console.log('res',res); }); ``` 5、拦截器 ```js // 请求拦截器 duanzi.interceptors.request.use( config => { console.log('请求拦截器', config); return config; }, error => { console.log('请求拦截器失败', error); } ); // 响应拦截器 duanzi.interceptors.response.use( response => { console.log('响应拦截器', response); return response; }, error => { console.log('响应拦截器失败', error); } ); ``` ### 三、