iframe调用页面添加header

  场景:对于iframe集成的页面进行权限校验。由于iframe默认的src的不能传递header, 因此需要把iframe调用方式改为ajax,代码如下

    function functionToExecute()
    {
    
    
        var url="此处填写iframe地址";
        $.ajax({
    
    
                type: 'GET',
                url: url,
                beforeSend: function (xhr) {
    
    
                    xhr.setRequestHeader('header里面的key', "value值");
                },
                success: function (data) {
    
    
                    $("#myiframe").css("display","block")
                    $("#myiframe").attr("src",url)
                },
               error:function (error)
                {
    
    
                    console.log(error);
                },
                statusCode: {
    
    
                    400: function () {
    
    
                        alert("server returned a bad request response.");
                    }
                }                
            });
    }

  写个测试的页面测试下

<iframe id="myiframe" style="position: fixed; z-index: 999; border-color:yellow; height: 50%; width: 50%; top: 20%; left:20%;filter: alpha(opacity=60); opacity: 0.6; -moz-opacity: 0.8;display:none"></iframe>
<button id="TestButton" onclick="functionToExecute()">click</button>

猜你喜欢

转载自blog.csdn.net/weixin_43839871/article/details/129042862