zoukankan      html  css  js  c++  java
  • 封装ajax

      var sendData = function(options) {
            var mat = options.url.match(/https?://([^/]+)/);
            if (mat && location.host != mat[0]) {
                jsonp(options);
            } else {
                ajax(options);
            }
        }
    
        var jsonp = function(options) {
            options = options || {};
            if (!options.url) {
                throw new Error("Parameter is not valid");
            }
            var callbackName = ('jsonp_' + Math.random()).replace(".", "");
            var oHead = document.getElementsByTagName('head')[0];       
            var oS = document.createElement('script');
            oHead.appendChild(oS);
            window[callbackName] = function(json) {
                oHead.removeChild(oS);
                clearTimeout(oS.timer);
                window[callbackName] = null;
                options.success && options.success(json);
            };
            if (options.url.indexOf('?') == -1) {
                oS.src = options.url + '?callback=' + callbackName;
            } else {
                oS.src = options.url + '&callback=' + callbackName;
            }
            if (options.time) {
                oS.timer = setTimeout(function() {
                    window[callbackName] = null;
                    oHead.removeChild(oS);
                    options.fail && options.fail({
                        message: "timeOut"
                    });
                }, time);
            }
        }
        var ajax = function(options) {
            options = options || {};
            options.type = (options.type || "GET").toUpperCase();
            options.dataType = options.dataType || "json";
            var params = formatParams(options.data);
            if (window.XMLHttpRequest) {
                var xhr = new XMLHttpRequest();
            } else {
                var xhr = new ActiveXObject('Microsoft.XMLHTTP');
            }
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4) {
                    var status = xhr.status;
                    if (status >= 200 && status < 300) {
                        options.success && options.success(xhr.responseText, xhr.responseXML);
                    } else {
                        options.fail && options.fail(status);
                    }
                }
            }
            if (options.type == "GET") {
                xhr.open("GET", options.url + "?" + params, true);
                xhr.send(null);
            } else if (options.type == "POST") {
                xhr.open("POST", options.url, true);
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send(params);
            }
        }
  • 相关阅读:
    ELK--filebeat命令行参数解释
    ELK--filebeat详解
    centOS7 修改DNS
    nginx-日志统计
    ceph 安装ceph问题汇总
    正则 挖网站表格复习
    c#反射优化 表达式树
    combotree 满足条件的节点不可选中
    NHibernate获取实体配置信息(表名,列名等等)
    jqgrid 单元格放超链接文本
  • 原文地址:https://www.cnblogs.com/xiaotaiyang/p/5577133.html
Copyright © 2011-2022 走看看