vue框架中的Axios封装
2024-08-26 19:04:35
function axios(options) {
let promise = new Promise((resolve, reject) => {
var xhr = new XMLHttpRequest();
var data = "";
//数据处理
for (var key in options.data) {
data += "&" + key + "=" + options.data[key]
}
if (options.method == "get") {
let url = options.url + "?" + data.slice(1);
xhr.open(options.method, url);
xhr.send();
} else if (options.method == "post") {
xhr.open(options.method, options.url);
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
xhr.send(data);
}
xhr.onreadystatechange = function () {
let timer = null;
let timeout = options.timeout?options.timeout:5000
if(xhr.readyState == 4 && xhr.status == 200){
let res = JSON.parse(xhr.responseText);
clearTimeout(timer);
resolve(res);
}
timer = setTimeout(()=>{
clearTimeout(timer);
reject(xhr.status);
},timeout)
}
})
return promise;
}
最新文章
- SpringBoot-Learning
- secure boot(安全启动)下为内核模块签名
- c# 中的日期格式
- IIS_Mvc发布
- nth-of-type和nth-child的区别
- iOS开发--iOS及Mac开源项目和学习资料
- Machine Learning for hackers读书笔记(七)优化:密码破译
- jquery 实现页面拖拽并保存到cookie
- C#使用Windows API 隐藏/显示 任务栏 (FindWindowEx, ShowWindow)
- MVC ValidationAttribute 服务器端自定义验证
- Android手势识别总结
- 第一周 动态规划Dynamic Programming(一)
- Generative Adversarial Nets[BEGAN]
- g4e基础篇#5 创建分支和保存代码
- 【C编程基础】C程序常用函数
- shell脚本学习系列之一---入门
- Python开发【模块】:aiohttp(一)
- 把目录C:\Python34\PCI_Code\chapter2\加到系统路径中
- [Android]Animation 动画介绍和实现
- WEB框架之Ajax
热门文章
- MAC Undefined symbols for architecture x86_64 cv::imwrite
- UOJ#75. 【UR #6】智商锁 随机化算法 矩阵树定理
- vue小技巧之偷懒的文件路径——减少不必要的代码
- Alpha冲刺(1/10)——2019.4.23
- MySQL系列
- 在NSMutableArray中添加空元素:NSNull类的使用
- window 安装redis、memcache的php扩展和 reidis 、memcache 及 reids管理软件
- Java实现生产者与消费者模式
- canvas生成海报
- XGBoost原理和公式推导