function myAxios(config) {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
if (config.params) {
const paramsObj = new URLSearchParams(config.params);
const queryString = paramsObj.toString();
config.url += `?${queryString}`;
}
xhr.open(config.method || 'GET', config.url);
xhr.addEventListener('loadend', () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(JSON.parse(xhr.response));
} else {
reject(new Error(xhr.response));
}
})
if (config.data) {
const jsonStr = JSON.stringify(config.data);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(jsonStr);
} else {
xhr.send();
}
})
}
封装自制axios
最新推荐文章于 2024-07-04 16:37:43 发布