npm i axios --save
import axios from 'axios' axios.defaults.baseURL = 'http://localhost:3000' axios.defaults.timeout = 3000
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
axios.get('/user?ID=12345') .then((response) => { // 处理成功情况 console.log(response); }) .catch((error) => { // 处理错误情况 console.log(error); }) .finally(() => { // 总是会执行 })
async function getUser() { try { const response = await axios.get('/user?ID=12345'); console.log(response); } catch (error) { console.error(error); } }
axios.get(baseURL + '/test') .then(res => { console.log('res', res.data); }) .catch(err => { console.log('err', err); }) .finally(() => { console.log('done for log'); })
axios.get(baseURL + '/test?id=123') .then(res => { console.log(res.data) })
axios.get(baseURL + '/test/123') .then(res => { console.log(res.data) })
适合传递多个数据,避免拼接错误
axios.get(baseURL + '/test', { params: { id: 112233 } }) .then(res => { console.log(res.data) })
router.get('/', (req, res) => { console.log(req.query); res.json({ code: 'ok', type: 'get query', data: req.query.id || 'hi, there.' }) }) router.get('/:id', (req, res) => { console.log('params', req.params); res.json({ code: 'ok', type: 'get params', data: req.params.id || 'hi, there.' }) })
传递JSON - 默认是 JSON 格式的数据,可以略去请求头 header 中的 content-type 设置
axios.post(baseURL + '/test', { id: 1, }) .then(res => console.log('res', res))
传递键值对key-value - 需要;略
现在都是通过数据绑定的形式传递对象|JSON
router.post('/', (req, res) => { console.log('req body', req.body); res.json({ code: 'ok', type: 'post', data: req.body.id || 'hi, there.' }) })
axios.delete(baseURL + '/test?id=123') .then(res => console.log(res.data))
axios.delete(baseURL + '/test/123') .then(res =>console.log(res.data))
axios.delete(baseURL + '/test', { params: { id: 123321 } }) .then(res => console.log(res.data))
router.delete('/', (req, res) => { console.log('query', req.query); res.json({ code: 'ok', type: 'delete query', data: req.query.id || 'hi, there.' }) }) router.delete('/:id', (req, res) => { console.log('params', req.params); res.json({ code: 'ok', type: 'delete', data: req.params.id || 'hi, there.' }) })
axios.put(baseURL + '/test/2147', { id: 123 }) .then(res => console.log(res))
router.put('/:id', (req, res) => { console.log('req body', req.body); console.log('req params', req.params); res.json({ code: 'ok', type: 'put', data: req.body.id || 'hi, there.' }) })
axios.interceptors.request.use( (config) => config, (error) => Promise.reject(error))
axios.interceptors.response.use( (response) => { return response; }, (error) => { return Promise.reject(error); });
序号 | 内容 | 难度 |
---|
//使用节点 for (let i = 0; i < res.data.length; i++) { let tr = document.createElement('tr') for (let item in res.data[i]) { let td = document.createElement('td') td.innerHTML = res.data[i][item] tr.appendChild(td) } axiosLocal.appendChild(tr) }
//使用innerHtml for (let i = 0; i < res.data.length; i++) { let tr = '' for (let item in res.data[i]) { tr += ' ' axiosLocal.innerHTML += tr }' + res.data[i][item] + ' ' } tr += '