原生ajax 封装

  // -------------------------ajax start-------------------------------
        ajax: function (url, data, type, backfun, errorbackfun, isabs) {
            var reqUrl = "/api/" + url;
            if (isabs) {
                reqUrl = url;
            }
            if (type.toUpperCase() != "POST") {
                type = "GET";
            }
            try {
                if (arguments.length == 3) {
                    //同步提交
                    var result = this.run_ajax_json(reqUrl, data, type);
                    return result;
                }
                else if (arguments.length > 3) {
                    //异步提交
                    this.run_ajax_async_json(reqUrl, data, type, backfun, errorbackfun);
                }
            }
            catch (err) {
                console.warn(err);
            }

        },
        run_ajax_json: function (url, data, type) {

            var vlMsg;
            var obpram = data;
            if (data == null || data == "")
                obpram = {};
            var tokenname = $("meta[name='cfname']").attr("content");
            var tokenvalue = $("meta[name='valuecs']").attr("content");
            $.ajax({
                url: url,
                data: obpram,
                type: type,
                beforeSend: function(xhr){
                    xhr.setRequestHeader(tokenname, tokenvalue);
                },
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                dataType: "json",
                async: false,
                success: function (result) {
                    vlMsg = result;
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    //console.log("调用ajax发生错误:" + thrownError + " 调用地址:" + url);
                    // xhr.responseJSON.message = xhr.responseJSON.message.replace('Authentication Failed:');
                    if (xhr.responseJSON && xhr.responseJSON.message) {
                        xhr.responseJSON.message = xhr.responseJSON.message.replace('Authentication Failed:','');
                    }
                    vlMsg = xhr.responseJSON;
                }
            });
            return vlMsg;
        },
        run_ajax_async_json: function (url, data, type, backfun, errorbackfun) {

            var obpram = data;
            if (data == null || data == "")
                obpram = {};
            var tokenname = $("meta[name='cfname']").attr("content");
            var tokenvalue = $("meta[name='valuecs']").attr("content");
            $.ajax({
                url: url,
                data: obpram,
                type: type,
                beforeSend: function(xhr){
                    xhr.setRequestHeader(tokenname, tokenvalue);
                },
                contentType: "application/x-www-form-urlencoded;charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if (backfun != null) backfun(result);
                },
                error: function (xhr, ajaxOptions, thrownError) {
                    //console.log("调用ajax发生错误:" + thrownError + " 调用地址:" + url);
                    if (errorbackfun != null && xhr.responseJSON && xhr.responseJSON.message) {
                        xhr.responseJSON.message = xhr.responseJSON.message.replace('Authentication Failed:','');
                    }
                    if (errorbackfun != null) {
                        errorbackfun(xhr.responseJSON);
                    }
                }
            });
        },
        // -------------------------ajax end-------------------------------

 

 

 

调用

this.ajax("url",{},"GET",function(result){


})

 

转载于:https://www.cnblogs.com/chen527/p/11473983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值