html 中 jquery的封装

jsonAjaxGet(url,type,param,successfn,errorfn){
            var _this = this ;
            $.ajax({
                type: type,
                url: _this.API_BASE_URL + url,
                data: param,
                dataType: 'json',
                headers:{
                    'authorization':window.localStorage.getItem('authorization'),
                    'userId':+(window.localStorage.getItem('id')),
                }, //请求头类型
                success: function(res){
                    successfn(res) ;   // 回调一个函数
                },
                error:function(err){
                    errorfn(err);      // 回调一个函数
                    console.log(err,'错误')
                }
            });
        },

// 不是json 格式
jsonAjax(url,type,param,successfn,errorfn){
            var _this = this ;
            $.ajax({
                type: type,
                url: _this.API_BASE_URL + url,
                data: JSON.stringify(param),
                dataType: 'text',
                headers:{
                    "content-type":"application/json",
                    'authorization':window.localStorage.getItem('authorization'),
                    'userId':+(window.localStorage.getItem('id')),
                }, //请求头类型
                success: function(res){
                    let jsonData = JSON.parse(res)
                    successfn(jsonData) ;
                },
                error:function(err){
                    errorfn(err);
                    console.log(err,'错误')
                }
            });
        },

作者:人参,每篇随笔皆原创(除非注明原作者的随笔),欢迎指正!

原文地址:https://www.cnblogs.com/panax/p/14437141.html