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;
}

最新文章

  1. SpringBoot-Learning
  2. secure boot(安全启动)下为内核模块签名
  3. c# 中的日期格式
  4. IIS_Mvc发布
  5. nth-of-type和nth-child的区别
  6. iOS开发--iOS及Mac开源项目和学习资料
  7. Machine Learning for hackers读书笔记(七)优化:密码破译
  8. jquery 实现页面拖拽并保存到cookie
  9. C#使用Windows API 隐藏/显示 任务栏 (FindWindowEx, ShowWindow)
  10. MVC ValidationAttribute 服务器端自定义验证
  11. Android手势识别总结
  12. 第一周 动态规划Dynamic Programming(一)
  13. Generative Adversarial Nets[BEGAN]
  14. g4e基础篇#5 创建分支和保存代码
  15. 【C编程基础】C程序常用函数
  16. shell脚本学习系列之一---入门
  17. Python开发【模块】:aiohttp(一)
  18. 把目录C:\Python34\PCI_Code\chapter2\加到系统路径中
  19. [Android]Animation 动画介绍和实现
  20. WEB框架之Ajax

热门文章

  1. MAC Undefined symbols for architecture x86_64 cv::imwrite
  2. UOJ#75. 【UR #6】智商锁 随机化算法 矩阵树定理
  3. vue小技巧之偷懒的文件路径——减少不必要的代码
  4. Alpha冲刺(1/10)——2019.4.23
  5. MySQL系列
  6. 在NSMutableArray中添加空元素:NSNull类的使用
  7. window 安装redis、memcache的php扩展和 reidis 、memcache 及 reids管理软件
  8. Java实现生产者与消费者模式
  9. canvas生成海报
  10. XGBoost原理和公式推导