$.ajaxSetup({}) --- js ajax统一配置

$.ajaxSetup() – 为将来的 AJAX 请求设置默认值(参数与$.ajax相同)

注意:封装了发这个函数的文件必须在每一个需要此默认配置的文件里引用该文件。并且引用在该文件的ajax文件之前。

常用的:

    url

规定发送请求的 URL。默认是当前页面的所有的ajax的url

    type

规定请求的类型(GET 或 POST)

    async

布尔值,表示请求是否异步处理。默认是 true

    contentType

发送数据到服务器时所使用的内容类型。默认是:"application/x-www-form-urlencoded"

    cache

布尔值,表示浏览器是否缓存被请求页面。默认是 true

    data

规定要发送到服务器的数据

    dataType

预期的服务器响应的数据类型

    timeout

设置本地的请求超时时间(以毫秒计)

    beforeSend(xhr)

发送请求前运行的函数

    success(result,status,xhr)

当请求成功时运行的函数

    error(xhr,status,error)

如果请求失败要运行的函数

    complete(xhr,status)

请求完成时运行的函数(在请求成功或失败之后均调用,即在 success 和 error 函数之后)

不常用的:

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

    jsonpCallback

在一个 jsonp 中规定回调函数的名称
$.ajaxSetup({
        cache: false,
        headers: {
            csrfToken: localStorage.getItem('csrfToken') || sessionStorage.getItem('csrfToken'),
        },
        success: function(result, data1, data2) {
            if (data2.getResponseHeader("csrfResult")) {
                csrfResult = data2.getResponseHeader("csrfResult");
            } else {
                return;
            }
            if (csrfResult == 505 && csrfResult == "505") {
                top.location.href = "/index.html";
            }
            // 获得指定的字段
            if (data2.getResponseHeader("csrfToken")) {
                csrfToken = data2.getResponseHeader("csrfToken");
            } else {
                return;
            }
            if (csrfToken != null && csrfToken != '') {
                top.location.href = "/index.html";
                localStorage.setItem('csrfToken', csrfToken);
            }
        },
        error: function(xmlhttprequest, textstatus) {
            $.busyLoadFull("hide");
            if (xmlhttprequest.status == 404) {
                // window.location = "/sys/404.html";
                top.location.href = "/sys/404.html";
            } else if (xmlhttprequest.status == 401 || xmlhttprequest.status == 0) {
                // window.location = "/index.html";
                top.location.href = "/login.html";
            } else if (xmlhttprequest.status == 601 || xmlhttprequest.status == 602 || xmlhttprequest.status == 603) {
                // window.location = "/sys/admin/admin.html";
                top.location.href = "/sys/admin/admin.html";
            } else if (xmlhttprequest.status == 403) {
                // window.location = "/sys/forbidden.html";
                top.location.href = "/sys/forbidden.html";
            }
        },
    });
};
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值