vue项目中的cookie带到 iframe中

   vue项目中嵌套了iframe标签,在iframe获取后台接口的时候报错,未登录,原因是找不到cookie。

  

<iframe :src='urlSrc' width='100%' height='500px'  id='content'></iframe>
this.urlSrc = `http://192.168.1.21:8080/gismap/?${document.cookie}`

然后在你iframe那个页面的js中获取到cookie

var cookie = window.location.search.slice(1)

获取到之后加入到ajax的请求头中

$.ajax({
   url:'后台接口路径',
   type:'post',
   data:{},
   dataType:'JSON',
   headers:{'Authorization':cookie},
   success:function(data){console.log(data)},
   error:function(error){console.log(error)}    
}) 

这样传过去之后就OK啦

原文地址:https://www.cnblogs.com/houBlogs/p/14927016.html