zoukankan      html  css  js  c++  java
  • vue框架中的Axios封装

     
    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;
    }
  • 相关阅读:
    Vue Cli3.0 使用jquery
    使用js加载器动态加载外部js、css文件
    通过js获取本机的IP地址
    $.ajax 中的contentType类型
    vue中 :style 与 :class 三元运算符使用
    bootstrap table checkbox获得选中得数据
    vscode自动生成文件头部注释和函数注释
    axios二次封装的几种方法
    vue组件库element-ui 的Table内容显示不更新
    Linux中iptables设置详细
  • 原文地址:https://www.cnblogs.com/xuazi-7an/p/10640416.html
Copyright © 2011-2022 走看看