• 跨域请求携带Cookie问题 和 layui框架的一些问题解决


    最近在使用layui时遇到一些跨域问题,比如上传或者table请求api数据,跨域请求没有暴露出来可以增加xhrFields: {withCredentials: true}的参数,这样服务端是取不到cookie的数据,不过可以曲线救国,比如把cookie数据通过参数传到服务端或者封装到head里,另外在网上找到一篇不错的,亲试有效

    原文链接:https://blog.csdn.net/qq_38261174/article/details/90405524

    详细见:https://www.cnblogs.com/nuccch/p/7875189.html

    或:https://harttle.land/2016/12/28/cors-with-cookie.html

    1.layui框架设置如下:

    layui.use(['form', 'laydate', 'table', 'upload'], function () {
                var form = layui.form,
                    laydate = layui.laydate,
                    table = layui.table,
                    upload = layui.upload,
                    $ = layui.$;
     
    //ajax全局参数设置
                $.ajaxSetup({
    //              dataType : "json",
    //              contentType : "application/json",
    //              headers : {
    //                    'Content-Type' : 'application/x-www-form-urlencoded'
    //               },
    //              同步
    //              async:false, // 默认true,异步
     
                    // 发送cookie
                    xhrFields: {
                        withCredentials: true
                    },
                    // 请求发送前
                    beforeSend: function () {
                        // 发送请求前,可以对data、url等处理
                    },
                    // 请求返回
                    complete: function () {
                        // 返回数据,根据数据调转页面等
                    }
                });
     
                // 渲染表数据   本来请求不带cookie,但上面设置了ajax全局参数,所以请求可带cookie
                table.render({
                    elem: '#table',
                    url: apiBaseUrl + ...
                    cols: tableTitles,
                    page: true
                    , text: '暂无相关数据'
                    , done: function (res, curr, count) {
                        layer.closeAll('loading');
                    }               
                });
     
     
     
    后台获取如下:
    HttpContext.Request.Cookies.TryGetValue("FaceUserKey", out string userNameOfEcrypt)

    2.通过请求头设置的方法如下:

    //ajax全局配置
            $.ajaxSetup({
                headers: {
                    "authorization": $.cookie("FeUserKey")
                },
                //xhrFields: {
                //    withCredentials: true
                //},
                //crossDomain: true
            });
     
     
     
    后台接收如下:
    var userNameOfEcrypt = HttpContext.Request.Headers["authorization"];
  • 相关阅读:
    WebRTC视频分辨率设置
    WebRTC本地插入多个转发节点,模拟多节点转发,造成延迟
    Android SeekBar 自定义thumb,thumb旋转动画效果
    WebRTC从摄像头获取图片传入canvas
    WebRTC本地分享屏幕,录制屏幕
    Android 悬浮窗
    WebRTC与音频音量
    WebRTC概念介绍
    Android CameraX ImageAnalysis 获取视频帧
    Android 摄像头预览悬浮窗
  • 原文地址:https://www.cnblogs.com/CuiRicky/p/11954532.html
Copyright © 2020-2023  润新知