zoukankan      html  css  js  c++  java
  • 【js】【Demo】js-ajaxProtogenesis

    js原生请求XMLHttpRequest封装

    //js原生请求XMLHttpRequest
    const ajaxProtogenesis = {
        post: function (url, data, success, error, headers) {
            //默认application/json
            if (!headers) {
                headers = { "Content-type": "application/json" };
            }
            this.ajax('post', url, true, data, success, error, headers);
        },
        get: function (url, success, error, headers) {
            this.ajax('get', url, true, null, success, error, headers);
        },
        ajax: function (method, url, async, data, success, error, headers, updateProgress) {
            //请求对象
            var xhr = new XMLHttpRequest();
    
            //请求类型,请求地址 .open(method, url, async, user, password);
            if (!method) { method = "post" }
            if (async == undefined || async == null) { async = true }
            xhr.open(method, url, async);
    
            xhr.responseType = 'json';//返回数据解析类型
    
            //状态事件
            xhr.onreadystatechange = function () {
                //readyState=4请求完成
                if (xhr.readyState == 4) {
                    var res = xhr.response;
    
                    if (xhr.status == 200) {
                        if (success) {
                            success(res);
                        }
                    } else if (error) {
                        error(res);
                    }
                }
            };
    
            //// 请求成功回调函数readyState等于4
            //xhr.onload = e => {
            //    console.log('request success');
            //    console.log(e.explicitOriginalTarget.responseText);
            //};
            //// 请求结束
            //xhr.onloadend = e => {
            //    console.log('request loadend');
            //    console.log(e.explicitOriginalTarget.responseText);
            //};
            //// 请求出错
            //xhr.onerror = e => {
            //    console.log('request error');
            //    console.log(e.explicitOriginalTarget.responseText);
            //};
            //// 请求超时
            //xhr.ontimeout = e => {
            //    console.log('request timeout');
            //    console.log(e.explicitOriginalTarget.responseText);
            //};
            //xhr.timeout = 0; // 设置超时时间,0表示永不超时
    
            //传输进度获取 //var updateProgress=function (event) {if (event.lengthComputable) { var completedPercent = event.loaded / event.total;  } }
            if (updateProgress) {
                //xhr.onprogress = updateProgress;当readyState等于3
                //xhr.upload.onprogress = updateProgress;
                xhr.upload.addEventListener("progress", updateProgress, false);
            }
    
            //请求头//formData参数不能设置请求头
            if (headers) {
                for (var i in headers) {
                    xhr.setRequestHeader(i, headers[i]);
                }
            }
    
            //json对象参数转json字符串
            if (headers && headers["Content-type"].toLowerCase() == "application/json") {
                if (typeof (data) == "object") {
                    data = JSON.stringify(data);
                }
            }
    
            //参数,发出请求xhr.send(null || new FormData || 'a=1&b=2' || 'json字符串');
            xhr.send(data);
    
            //终止请求xhr.abort();
        },
        formData: function (url, data, success, error, updateProgress) {
            //data例{ planId: planId, file: document.getElementById("fileUpload").files[0] }
            //updateProgress传输进度回调
            let formData = new FormData();
            for (var i in data) {
                formData.append(i, data[i]);
            }
    
            //formData参数不能设置请求头
            this.ajax("POST", url, true, formData, success, error, null, updateProgress);
        }
    }
  • 相关阅读:
    Java匹马行天下之C国程序员的秃头原因
    Java匹马行天下之学编程的起点——高级语言大锅烩
    jdbc连接案例
    Java匹马行天下之学编程的起点——走进编程的殿堂
    Java匹马行天下之学编程的起点——编程常识知多少
    cookie、session和application超详解说
    匹马行天下之博客园五月纪念日——去你的写博无用论
    匹马行天下之高薪编程,品牌公司——人往高处走,作为程序员的你够格吗
    MySQL中boolean类型设置
    python使用rsa库做公钥解密(网上别处找不到)
  • 原文地址:https://www.cnblogs.com/lanofsky/p/15233578.html
Copyright © 2011-2022 走看看