• 程序ajax请求公共组件-- app-jquery-http.js


    $.HTTP =
    {
        getUrlParam : function (name)
        {
            var reg = new RegExp ("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr (1).match (reg);
            if (r != null)
                return unescape (r[2]);
            return null;
        },
        /**
         * 
         * @param opt
         *            opt.url,postdata, success, failure, error
         */
        obj : function (opt)
        {
            var formData;
    
            if (opt.postType) {
                switch (opt.postType) {
                case "multipart":
                    formData = new FormData($(opt.formId)[0]);
                    break;
                case "form":
                    formData =  $.param(opt.ajaxData);
                    break;
                case "json":
                    formData = JSON.stringify(opt.ajaxData);
                    break;
                default:
                    return;
                }
            } else {
                formData = opt.ajaxData;
            }
            
            var sopt =
            {
                type : opt.type == undefined ? "post" : opt.type,
                async : false,
                url : opt.url,
                data : formData,
                dataType : "json",
                success : function (json)
                {
                    if (json.stat == 1)
                    {
                        if (opt.success != undefined)
                            opt.success (json.data);
                    }
                    else
                    {
                        if (opt.failure != undefined)
                            opt.failure (json);
                        else if (json.code != undefined)
                        {
                            $.HTTP.show_code_err (json);
                        }
                        else
                        {
                            console.log ("success不等于true   【url: " + opt.url + "】");
                            if (json.errorMessages != null && json.errorMessages.length > 0)
                            {
                                $.NOTIFY.showNotice ("错误", json.errorMessages[0]);
                            }
                        }
                        
                    }
                    
                },
                error : function (XMLHttpRequest, textStatus, errorThrown)
                {
                    var info = "XMLHttpRequest:" + JSON.stringify (XMLHttpRequest) + " ;textStatus:" + textStatus
                            + "; errorThrown:" + JSON.stringify (errorThrown) + ";   【" + opt.url + "】";
                    console.log (info);
                    if (opt.error != undefined)
                        opt.error (XMLHttpRequest, textStatus, errorThrown);
                    else
                    {
                        $.NOTIFY.showError ("请求错误", "系统发生请求错误,请联系管理员解决。");
                    }
                }
            };
            
            if (opt.postType) {
                switch (opt.postType) {
                case "multipart":
                    sopt.async = false;
                    sopt.cache = false;
                    sopt.contentType = false;
                    sopt.processData = false;
                    break;
                case "form":
                    sopt.contentType = 'application/x-www-form-urlencoded';
                    break;
                case "json":
                    sopt.contentType = 'application/json';
                    sopt.dataType = "json";
                    break;
                default:
                    return;
                }
            }
            $.ajax (sopt);
        },
        list : function (opt)
        {
            var formData;
    
            if (opt.postType) {
                switch (opt.postType) {
                case "multipart":
                    formData = new FormData($(opt.formId)[0]);
                    break;
                case "form":
                    formData =  $.param(opt.ajaxData);
                    break;
                case "json":
                    formData = JSON.stringify(opt.ajaxData);
                    break;
                default:
                    return;
                }
            } else {
                formData = opt.ajaxData;
            }
            
            var sopt =
            {
                type : opt.type == undefined ? "get" : opt.type,
                async : false,
                url : opt.url,
                data : opt.ajaxData,
                dataType : "json",
                success : function (json)
                {
                    if (json.stat == 1)
                    {
                        if (opt.success != undefined)
                            opt.success (json.list, json.pageInfo);
                    }
                    else
                    {
                        if (opt.failure != undefined)
                            opt.failure (json);
                        else if (json.code != undefined)
                        {
                            $.HTTP.show_code_err (json);
                        }
                        else
                        {
                            console.log ("success不等于true   【url: " + opt.url + "】");
                            if (json.errorMessages != null && json.errorMessages.length > 0)
                            {
                                $.NOTIFY.showNotice ("错误", json.errorMessages[0]);
                            }
                        }
                    }
                    
                },
                error : function (XMLHttpRequest, textStatus, errorThrown)
                {
                    var info = "XMLHttpRequest:" + JSON.stringify (XMLHttpRequest) + " ;textStatus:" + textStatus
                            + "; errorThrown:" + JSON.stringify (errorThrown) + ";   【" + opt.url + "】";
                    console.log (info);
                    if (opt.error != undefined)
                        opt.error (XMLHttpRequest, textStatus, errorThrown);
                    else
                    {
                        $.NOTIFY.showError ("请求错误", "系统发生请求错误,请联系管理员解决。");
                    }
                }
            };
            
            if (opt.postType) {
                switch (opt.postType) {
                case "multipart":
                    sopt.async = false;
                    sopt.cache = false;
                    sopt.contentType = false;
                    sopt.processData = false;
                    break;
                case "form":
                    sopt.contentType = 'application/x-www-form-urlencoded';
                    break;
                case "json":
                    sopt.contentType = 'application/json';
                    sopt.dataType = "json";
                    break;
                default:
                    return;
                }
            }
            
            $.ajax (sopt);
        },
        show_code_err : function (json)
        {
            if (json.code == 1403)
            {
                $.NOTIFY.showLogin ();
                return;
            }
            else if (json.errorMessages != undefined && json.errorMessages != null && json.errorMessages.length > 0)
            {
                $.NOTIFY.showNotice ("错误", json.errorMessages[0]);
            }
            
        }
    }
  • 相关阅读:
    从0开始学Swift笔记整理(二)
    从0开始学Swift笔记整理(一)
    JAVA反射机制
    Spring基础知识汇总
    关于类和对象的进一步讨论 C++
    C++ 共用体 枚举类型 所有
    自定义数据类型 C++ 结构体类型 共同体类型 枚举类型 类类型{}
    C++ 指针 引用 变量引用
    函数和指针 C++
    C++字符串与指针 所有的内容也就这么多了。
  • 原文地址:https://www.cnblogs.com/cryst/p/6029139.html
Copyright © 2020-2023  润新知