zoukankan      html  css  js  c++  java
  • 原生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){


    })
  • 相关阅读:
    SpringBoot项目启动遇到的问题记录
    关于点击按钮提交前进行数据校验
    idea插件的位置
    sqlite
    xamarin.forms 使用依赖注入
    vs2019查找替换,使用正则表达式功能
    EFCore 事务提交
    windows磁盘【文件和文件夹遇到-打不开or不能删除or损坏】的解决方式
    xamarin.forms 中使用Forms9Patch插件来显示图片遇到的问题
    net5 依赖注入的时候,遇到的问题:Cannot consume scoped service from singleton IHostedService
  • 原文地址:https://www.cnblogs.com/chen527/p/11473983.html
Copyright © 2011-2022 走看看