Axios Basic
November 13, 2023Less than 1 minute
The reason for AXIOS
- 在浏览器中发送XMLHttpRequests请求
- 在node.js中发送http请求
- 支持Promise API
- 拦截请求和响应
- 转换请求和响应数据
- etc.
Requests of AXIOS
axios(config)
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.post(url[data[, config]])
axios.put(url[data[, config]])
axios.patch(url[data[, config]])
Examples of AXIOS
axios发送请求, axios本身支持Promise,可使用Promise链式编程的方式进行书写
Single Request
//axios发送get请求示例
axios({
url: 'http://httpbin.org/get',
params: {
type: 'pop',
page: 1
}
}).then(res => {
console.log(res);
})
//axios发送post请求示例
axios({
url: 'http://httpbin.org/post',
params: {
type: 'pop',
page: 1
},
method: 'post'
}).then(res => {
console.log(res);
})
Concurrent
//和Promise并发请求方式保持一致
axios.all([
axios(),
axios(),
]).then(res => {
})
// Results delay
//和Promise并发请求方式保持一致
axios.all([
axios(),
axios(),
]).then(axios.spread((res1, res2) => {
//这里对两次请求结果直接操作
}))