当前位置:网站首页>偶然发现了另一种跨域方式,不知道有没有人这么玩过
偶然发现了另一种跨域方式,不知道有没有人这么玩过
2022-06-29 19:58:00 【全栈程序员站长】
众所周知,jsonp利用浏览器不限制加载跨域外部资源的特性,使用script标签src属性请求接口,加载数据,在script中触发提前声明好的函数拿到数据,前端的前辈们这一手操作简直就是犀利。其实实现单向的接口调用使用任何资源标签都可以实现,关键在于如何取到后端传回的数据实现双向交互。 现代浏览器提供了getComputedStyle这个API,使得通过js可以拿到某个元素最终应用的css样式。(低版本IE中为currentStyle)。如下所示:
如此一来,我们就可以在css样式中取得想要的数据文本 1.利用伪类的::after或::before的 { content:”{数据}” } 或者利用 font-family:”{数据}”
data.css 代码
.data-div{
font-family: "{a:'1'}";
}2.动态加载css样式,并监听其load事件,我这里用的是静态文件(上边的data.css),有兴趣的可以写个接口试下。
<div class="data-div" style="width:50px;height:50px;background:red;"></div>
<script>
div = document.getElementsByClassName("data-div")[0] //应用样式的数据标签
div.onclick = function(){
var link = document.createElement("link");
link.rel = "stylesheet"
link.href = "/data.css"
link.type = "text/css"
link.onload = function(){
console.log(window.getComputedStyle(div)["fontFamily"]); //获取写在font-family属性的数据
}
document.head.appendChild(link)
}
</script> 3.点击数据标签,可以看到输出结果如下:
PS:这个方法和JSONP一样需要后端配合将数据按一定的规则拼接给前端,其实放到现在并没什么卵用,现在已经很少有看到用JSONP实现跨域请求的。但是自己玩一玩还是可以的,顺便致敬一波JSONP,致敬一波前辈们
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/101349.html原文链接:https://javaforall.cn
边栏推荐
- PHP implementation extracts non repeated integers (programming topics can be the fastest familiar functions)
- JVM (3) class loading
- npm ERR! fatal: early EOF npm ERR! fatal: index-pack failed
- Dynamics CRM: 本地部署的服务器中, Sandbox, Unzip, VSS, Asynchronous还有Monitor服务的作用
- data link layer
- Etcd database source code analysis - put process of server
- How to set a pod to run on a specified node
- 剑指 Offer 66. 构建乘积数组
- One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform
- The concept and properties of mba-day26 number
猜你喜欢

关于印发宝安区重点产业项目和总部项目遴选及用地保障实施细则(2022修订版)的通知

KDD 2022 | 協同過濾中考慮錶征對齊和均勻性

One hour to build a sample scenario sound network to release lingfalcon Internet of things cloud platform

Foxit software was invited to appear at the 2022 advanced manufacturing digital intelligence development forum

Koa source code analysis

Flume configuration 2 - ganglia for monitoring
![[boutique] detailed explanation of Pinia](/img/94/d332e32dba54be3c2d3f6ff08a85fa.png)
[boutique] detailed explanation of Pinia

How to set a pod to run on a specified node

如何设置 Pod 到指定节点运行

【U盘检测】为了转移压箱底的资料,买了个2T U盘检测仅仅只有47G~
随机推荐
MySQL remote connection
攻防演练中的防守基石——全方位监控
Connaissance générale des paramètres de sécurité du serveur Cloud
KDD 2022 | characterization alignment and uniformity are considered in collaborative filtering
软件测试逻辑覆盖相关理解
Finally, Amazon~
Nacos problem
[network orientation training] - Enterprise Park Network Design - [had done]
Linux Installation mysql8
【观察】软通动力刘天文:拥抱变化“顺势而为”,做中国数字经济“使能者”...
云服务器的安全设置常识
测试方法学习
proxmox集群节点崩溃处理
What is a database? Database detailed notes! Take you into the database ~ you want to know everything here!
1404萬!四川省人社廳關系型數據庫及中間件軟件系統昇級采購招標!
Zotero journal automatic matching update influence factor
Common knowledge of ECS security settings
data link layer
NLP - GIZA++ 实现词对齐
Flume configuration 3 - interceptor filtering