window.name实现ajax跨域post请求

首先不得不佩服想出用window.name来传递和保存数据的人!

 

项目中需要用到ajax跨域作post请求,原本以为是没法解决的问题,没想到找到了window.name

 

http://www.blueidea.com/tech/web/2008/6147.asp

 

window.name 传输技术,原本是 Thomas Frank 用于解决 cookie 的一些劣势(每个域名 4 x 20 Kb 的限制、数据只能是字符串、设置和获取 cookie 语法的复杂等等)而发明的(详细见原文:《Session variables without cookies》),后来 Kris Zyp 在此方法的基础上强化了 window.name 传输 ,并引入到了 Dojo dojox.io.windowName),用来解决跨域数据传输问题。

 

window.name 的美妙之处:name 值在不同的页面(甚至不同域名)加载后依旧存在,并且可以支持非常长的 name 值(2MB)。

大致原理就是:

1, 请求前,在页面上创建一个iframe,地址是被请求的页面

2, 将请求post到该iframe

3, 服务器端将数据写入window.name, 实际上写入到了页面上新建的这个iframe的window.name

4, 修改iframe的src(window.name是不变的,关键),改成同域的某个地址,比如:http://yourdomain.com/robots.txt

5, 直接访问该iframe的window.name获取数据

 

window.name的jquery实现:

 

http://plugins.jquery.com/project/windowNameTransport

 

(不知道为什么jquery的实现代码量那么大)

 

首先需要在域名根目录下放置一个空文件robots.txt (实际上可以为任意文件,但一般都会有robots.txt或crossdomain.xml这几个文件)

 

代码片段:

 

在 React TSX 中基于 `iframe` 实现 AJAX 请求,可按如下步骤进行: ### 1. 创建 `iframe` 元素 借助 React 的 `useRef` 钩子来创建 `iframe` 元素的引用,然后在组件挂载时插入 `iframe` 到 DOM 里。 ### 2. 与 `iframe` 进行通信 利用 `window.postMessage` 方法达成父窗口和 `iframe` 之间的通信。 ### 3. 在 `iframe` 中发起请求 在 `iframe` 里发起请求,并且把结果传递回父窗口。 以下是一个示例代码: ```tsx import React, { useRef, useEffect } from 'react'; const CrossDomainRequest: React.FC = () => { const iframeRef = useRef<HTMLIFrameElement>(null); useEffect(() => { if (iframeRef.current) { const iframe = iframeRef.current; // 设置 iframe 的源 iframe.src = 'https://example.com/iframe.html'; // 监听来自 iframe 的消息 const handleMessage = (event: MessageEvent) => { if (event.origin === 'https://example.com') { // 处理来自 iframe 的响应数据 console.log('Received data from iframe:', event.data); } }; window.addEventListener('message', handleMessage); return () => { window.removeEventListener('message', handleMessage); }; } }, []); const sendRequest = () => { if (iframeRef.current) { const iframe = iframeRef.current; // 向 iframe 发送消息,请求数据 iframe.contentWindow?.postMessage('Request data', 'https://example.com'); } }; return ( <div> <button onClick={sendRequest}>Send Cross-Domain Request</button> <iframe ref={iframeRef} style={{ display: 'none' }} /> </div> ); }; export default CrossDomainRequest; ``` ### `iframe.html` 文件示例 ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>IFrame for Cross-Domain Request</title> </head> <body> <script> window.addEventListener('message', (event) => { if (event.origin === 'https://your-react-app.com') { // 发起请求 const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://api.example.com/data', true); xhr.onreadystatechange = () => { if (xhr.readyState === 4 && xhr.status === 200) { // 将响应数据发送回父窗口 event.source.postMessage(xhr.responseText, event.origin); } }; xhr.send(); } }); </script> </body> </html> ``` ### 代码解释 1. **创建 `iframe` 元素**:运用 `useRef` 钩子创建 `iframe` 元素的引用,在组件挂载时设置 `iframe` 的源。 2. **监听消息**:在父窗口监听 `message` 事件,处理来自 `iframe` 的响应数据。 3. **发送消息**:通过 `postMessage` 方法向 `iframe` 发送请求消息。 4. **在 `iframe` 中发起请求**:在 `iframe` 里监听 `message` 事件,接收到请求消息后发起请求,将响应数据发送回父窗口。 ### 注意事项 - 要保证 `iframe` 的源和父窗口的源不同,以此模拟请求。 - 在 `postMessage` 方法中指定目标源,增强安全性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值