Ext 重写url请求,对请求做统一处理

参考大神文档:https://blog.csdn.net/xmtblog/article/details/23434087
Ext.override(Ext.Ajax, {
    request: function(options) {
        options = options || {};
        var me = this,
            scope = options.scope || window,
            username = options.username || me.username,
            password = options.password || me.password || ‘‘,
            async,
            requestOptions,
            request,
            headers,
            xhr;
        if (me.fireEvent(‘beforerequest‘, me, options) !== false) {
            requestOptions = me.setOptions(options, scope);
            if (me.isFormUpload(options)) {
                me.upload(options.form, requestOptions.url, requestOptions.data, options);
                return null;
            }
            if (options.autoAbort || me.autoAbort) {
                me.abort();
            }
            var nParam = options.params;
            if (nParam != undefined) {

                var arrParam = objTransArray(nParam,requestOptions.url);
                var newP = newParamBuilder(arrParam);
                var sign = signBuild(newP);
                requestOptions.data = newP;
                var headers = new Object();
                headers.signnn = sign;
                options.headers= headers;

            }
            async = options.async !== false ? (options.async || me.async) : false;
            xhr = me.openRequest(options, requestOptions, async, username, password);
            headers = me.setupHeaders(xhr, options, requestOptions.data, requestOptions.params);

            request = {
                id: ++Ext.data.Connection.requestId,
                xhr: xhr,
                headers: headers,
                options: options,
                async: async,
                timeout: setTimeout(function() {
                    request.timedout = true;
                    me.abort(request);
                }, options.timeout || me.timeout)
            };
            me.requests[request.id] = request;
            me.latestId = request.id;
            if (async) {
                xhr.onreadystatechange = Ext.Function.bind(me.onStateChange, me, [request]);
            }
            xhr.send(requestOptions.data);
            if (!async) {
                return me.onComplete(request);
            }
            return request;
        } else {
            Ext.callback(options.callback, options.scope, [options, undefined, undefined]);
            return null;
        }
    }
});

/**
 * obj对象转为数组
 * @param obj
 */
function objTransArray(obj,url){
    //将object对象转为 array数组
    var array = $.map(obj,function(value,key){
        return [key+"="+value];
    });
    var timestamp = new Date().getTime();
    array.push("timestamp="+timestamp);
    var urlArr  = url.split("?");
    if(urlArr[1] != undefined){
        var subUrlArr = urlArr[1].split("&");
        for(var i=0; i< subUrlArr.length;i++){
            array.push(subUrlArr[i])
        }
    }
    array.sort();//ascii码排序 从小-大
    return array;
}

function newParamBuilder(arr){
    var param = "";
    for(var i = 0;i < arr.length; i++) {
        if(i + 1 == arr.length) {
            param += arr[i] ;
        }else{
            param += arr[i] + "&";
        }
    }
    return param;
}

/**
 * 生成校验值
 * @param arr
 * @returns {*}
 */
function signBuild(param){
    return $.md5(param);
}
  

  

Ext 重写url请求,对请求做统一处理

上一篇:php大文件(视频)切片上传


下一篇:Kubernetes - Error from server (InternalError): an error on the server ("") has prevented the request from succeeding (get nodes)