jquery ajax设置header的两种方式

photo_2023-11-07_20-06-07.jpg anjingling.png 科学上网教程 | 机场推荐 | 本站商务合作

1.使用headers参数

$.ajax({
    url: interface_getDwjbxx,
    async: false,
    type: "GET",
    data: {"dwId": urlParamsJson.dwId},
    headers: {'Authorization': layui.sessionData("checkModel").Authorization},
    dataType: "json",
    success: function (result) {
        // 基本信息
        if (result.code == 1000) {
            if(result.data){
                template.defaults.escape = false;
                var html = template('unit_info_template', result.data);
                document.getElementById('unitBaseInfo').innerHTML = html;
            }
        } else {
            layer.alert('查询单位基本信息出错!');
        }
    },
    error: function (error) {
        layer.alert("查询单位基本信息出错!");
    }
});

2.使用beforeSend方法

$.ajax({
    url: interface_getDwjbxx,
    async: false,
    type: "GET",
    data: {"dwId": urlParamsJson.dwId},
    dataType: "json",
    beforeSend: function(request) {
         request.setRequestHeader("Authorization", layui.sessionData("checkModel").Authorization);
    },
    success: function (result) {
        // 基本信息
        if (result.code == 1000) {
            if(result.data){
                template.defaults.escape = false;
                var html = template('unit_info_template', result.data);
                document.getElementById('unitBaseInfo').innerHTML = html;
            }
        } else {
            layer.alert('查询单位基本信息出错!');
        }
    },
    error: function (error) {
        layer.alert("查询单位基本信息出错!");
    }
});
赞(0)
未经允许禁止转载:优米格 » jquery ajax设置header的两种方式

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册

Ads Blocker Image Powered by Code Help Pro

检测到广告已被屏蔽

您好,系统检测到您使用了广告屏蔽组件。

本网站并不包含任何侵入性广告,我们提供免费的内容,但是需要广告收入来维持运营,还请将本站加入拦截白名单!