FE.B-理解浏览器的同源策略与跨域方案

本文详细解析了浏览器的同源策略,包括其概念、目的及限制行为,并提供了多种跨域通信和请求的解决方案,如JSONP、CORS、WebSocket及通过服务器代理等方法。

同源

概念:协议,域名,端口 相同。
目的:保证用户信息的安全,防止恶意的网站窃取数据。
限制的行为

  • Cookie、LocalStorage 和 IndexDB 无法读取。
  • DOM 无法获得。
  • AJAX 请求不能发送。

跨域

  1. 资源跳转: A链接、重定向、表单提交
  2. 资源嵌入: <link>、<script>、<img>、<frame>等dom标签,还有样式中background:url()、@font-face()等文件外链
  3. 脚本请求: js发起的ajax请求、dom和js对象的跨域操作等

解决方案

  • 场景

    • 两个网页一级域名相同,二级域名不同,需要共享 Cookie
    • iframe窗口和window.open的窗口,需要与父窗口通信。
  • 方案

    • 浏览器设置一级域名。document.domain = 'example.com';
    • 服务器设置一级域名。Set-Cookie: key=value; domain=.example.com; path=/

  • 场景

    • 完全不同源的网站,需要窗口通信
  • 方案

    • 父子窗口(互相)写location.hash,(互相)监听hashchange
    • 子窗口写window.name后跳回同域,父窗口读window.name
    • 浏览器跨文档通信 window.postMessage

  • 场景

    • AJAX请求非同源地址
  • 方案

    • 架设服务器代理
    • JSONP
    • WebSocket
    • CORS

参考资料
浏览器同源政策及其规避方法 - 阮一峰
前端常见跨域解决方案(全)
跨域几种方式

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>模型性能展示</title> <style> body { font-family: Arial, sans-serif; max-width: 1000px; margin: 0 auto; padding: 20px; color: #333; } h1 { color: #1a73e8; border-bottom: 2px solid #1a73e8; padding-bottom: 10px; } .chip-selection { margin: 20px 0; padding: 15px; background-color: #f5f5f5; border-radius: 5px; } .chip-option { display: inline-block; margin-right: 20px; padding: 10px 20px; background-color: #e8f0fe; border-radius: 5px; cursor: pointer; transition: all 0.3s; } .chip-option:hover { background-color: #d2e3fc; } .chip-option.selected { background-color: #1a73e8; color: white; } .group-section { margin-top: 30px; display: none; } .group-title { font-size: 18px; font-weight: bold; margin-bottom: 15px; color: #1a73e8; } .group-items { display: flex; flex-wrap: wrap; gap: 10px; } .group-item { padding: 8px 15px; background-color: #f1f3f4; border-radius: 5px; cursor: pointer; } .group-item:hover { background-color: #e0e0e0; } .footer { margin-top: 40px; font-size: 12px; color: #666; text-align: center; } </style> </head> <body> <h1>模型性能</h1> <div class="chip-selection"> <h2>节点性能</h2> <div> <div class="chip-option" onclick="selectChip('single')">BS9SX1A单芯片</div> <div class="chip-option" onclick="selectChip('double')">Ascend610Lite双芯片</div> </div> </div> <div id="group-section" class="group-section"> <div class="group-title">分组</div> <div class="group-items"> <div class="group-item" onclick="showGroupInfo('model')">模型名</div> <div class="group-item" onclick="showGroupInfo('size')">模型大小</div> <div class="group-item" onclick="showGroupInfo('frame')">帧率</div> <div class="group-item" onclick="showGroupInfo('core')">核数</div> <div class="group-item" onclick="showGroupInfo('delay')">时延</div> <div class="group-item" onclick="showGroupInfo('bandwidth')">带宽</div> <div class="group-item" onclick="showGroupInfo('quantization')">量化率</div> </div> </div> <div class="footer"> <p>Huawei Proprietary - Restricted Distribution</p> <p id="current-date"></p> </div> <script> // 显示当前日期 const now = new Date(); const options = { year: 'numeric', month: 'long', day: 'numeric' }; document.getElementById('current-date').textContent = now.toLocaleDateString('zh-CN', options); // 选择芯片类型 function selectChip(type) { // 更新选中状态 const options = document.querySelectorAll('.chip-option'); options.forEach(opt => opt.classList.remove('selected')); if(type === 'single') { options[0].classList.add('selected'); } else { options[1].classList.add('selected'); } // 显示分组区 document.getElementById('group-section').style.display = 'block'; } // 显示分组信息 function showGroupInfo(group) { // 这里可以添加显示具体分组信息的逻辑 alert('您选择了: ' + group + ' 分组\n实际应用中这里会显示详细数据'); } </script> </body> </html>我该如何使用这个html脚本后端python脚本进行交互?
07-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值