跨域访问header的问题

本文介绍了解决跨域请求中返回流数据时遇到的Refused to get unsafe header错误的方法。通过在IIS服务器响应头中添加Access-Control-Expose-Headers来允许客户端获取指定的头部信息,同时提供了服务器端及客户端的具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在服务器上要返回流数据到客户端,需要添加header,从而在客户端访问,但是访问时老是弹出“Refused to get unsafe header "*****"”问题,需要在iis的响应表头里加上



Access-Control-Expose-Headers:你的表头(Access-Control-Request-Headers:你的表头,可不加)。


另:附上传输流的代码:

服务器端:

   <WebMethod()>
    Public Sub getAttatchMents(ByVal url As String, ByVal oid As Integer)
        Dim ptask As System.Threading.Tasks.Task = System.Threading.Tasks.Task.Run(Async Function()
                                                                                       Await loadlll(url, oid)
                                                                                   End Function)
        ptask.Wait()
      
        If _attachMents.ContainsKey(oid) = False Then
            Return
        End If        
        For Each attach As MyAttachMentValue In _attachMents.Item(oid)
          
            Context.Response.AddHeader("Authorization", attach.name)
            Context.Response.BinaryWrite(attach.value)
            Context.Response.Flush()
        Next
      
    End Sub

客户端(通过arcgis js 访问):

esriRequest("http://localhost:88/EsriDataService.asmx/getAttatchMents",{
    responseType:"blob",
    method:"post",
    timeout:0,
    headers:{"Authorization":""},
    query:{
        "url":layer.url + "/" + layer.layerId,
        "oid":bh
    }
}).then(lang.hitch(this,this.showFile)).otherwise( function(error) {
    console.error("get Attachments failed. ", error);
});
showFile:function(response)
{
    var data = response.data;
   var header = response.getHeader("Authorization");
    blobToDataURL(data,lang.hitch(this, function (dataurl) { 
        var node = document.createElement("img");
        node.setAttribute("src",dataurl);
       // node.style.transform = "rotate(90deg)";
        node.style.width = "100%";
        node.style.height = "100%";
        this.ljcontent.appendChild(node);
     
    }));
    //**blob to dataURL**
    function blobToDataURL(blob, callback) {
        var a = new FileReader();
        a.onload = function (e) { callback(e.target.result); }
        a.readAsDataURL(blob);
    }
},


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值