axios的简单封装
MoMaek
共 3453字,需浏览 7分钟
· 2021-03-09
html中简单封装axios
/**
* @author [Moisten] itcode.icu
* @version 1.0.0
* @overview 封装axios请求
*
* 调用方法:
* var datas = values;
* axiosPostRequst(`register`, datas).then(res => { console.log('post请求--->', res) });
*
*/
// 默认环境配置
axios.defaults.baseURL = 'http://192.168.0.102/Home/Index/'
// 请求超时设置
axios.defaults.timeout = 50000
// post请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
// 请求拦截器
axios.interceptors.request.use(config => {
return config
})
// post请求
function axiosPostRequst(url, data) {
return new Promise((resolve, reject) => {
axios
.post(url, data)
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
// get请求
function axiosGetRequst(url) {
return new Promise((resolve, reject) => {
axios
.get(url)
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err.data)
})
})
}
vue-cli下简单的封装axios
- 安装
axios
npm install axios --save
- 在
main.js
引入axios
import axios from 'axios'
Vue.prototype.$axios = axios
//默认请求头
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
- 使用
axios
//拼接参数
this.$axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
//对象传参
this.$axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
//或者使用箭头函数
this.$axios.get('/user?ID=12345')
.then((response) => {
console.log(response);
})
.catch((error) => {
console.log(error);
});
//执行多个并发请求
function getUserAccount() {
return this.$axios.get('/user/12345');
}
function getUserPermissions() {
return this.$axios.get('/user/12345/permissions');
}
axios.all([getUserAccount(), getUserPermissions()])
.then(this.$axios.spread(function (acct, perms) {
//两个请求现已完成
}));
- 新建文件封装
axios
//引入axios
import axios from 'axios'
//通过promise 封装
export function request(config){
return new Promise((resolve, reject) => {
//创建axios实例
const instance = axios.create({
baseURL:'http://localhost:8080/api',
timeout:5000
})
//发送请求
instance(config)
.then(res => {
resolve(res);
})
.catch(err => {
reject(err);
})
})
}
- 引用
axios
import { request } from '../../api/request'
request({
url: '/showbanners/index',
method: 'get'
})
.then((res) => {
console.log(res)
})
.catch((err) => {
console.log(err)
})
- 添加拦截器
import axios from 'axios'
//通过promise封装
export function request (config) {
return new Promise((resolve, reject) => {
//创建axios实例
const instance = axios.create({
baseURL: 'http://localhost:8080/api',
timeout: 5000
})
// 请求拦截
instance.interceptors.request.use(config => {
return config
},
err => {
console.log(err)
})
// 响应拦截
instance.interceptors.response.use(result => {
console.log('拦截数据:=>', result)
return result
},
err => {
console.log(err)
})
//发送请求
instance(config)
.then(res => {
resolve(res)
})
.catch(err => {
reject(err)
})
})
}
评论
xr-httpXHR 的简单封装
xr是XMLHttpRequest的超简单封装,并返回ES6Promise。示例代码:xr.get('/api/items', {take: 5}) .then(res => console.
xr-httpXHR 的简单封装
0
asyncCoreDataWrapperCoreData 的简单封装
一个 CoreData 的简单封装,支持多线程操作,杜绝阻塞,可以更加开心地和 CoreData 一
asyncCoreDataWrapperCoreData 的简单封装
0
asyncCoreDataWrapperCoreData 的简单封装
一个CoreData的简单封装,支持多线程操作,杜绝阻塞,可以更加开心地和CoreData一起玩耍。
asyncCoreDataWrapperCoreData 的简单封装
0
KeychainAccess简单的 Keychain 封装
KeychainAccess 是 Keychain 简单的 Swift 封装,支持 iOS 和 OS
KeychainAccess简单的 Keychain 封装
0