跨域通信的几种方式(jsonp,hash,postMessage,websocket,cors)
来源:互联网 发布:ewsa软件字典手机号码 编辑:程序博客网 时间:2024/05/21 07:46
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>通信类</title> </head> <body> <script type="text/javascript"> // 创建ajax【参考网址】https://segmentfault.com/a/1190000006669043 // 参考jsonp.js </script> <script src="http://www.abc.com/?data=name&callback=jsonp" charset="utf-8"></script> <script type="text/javascript"> // jsonp({ // data: { // // }, // }); </script> <script type="text/javascript"> /** * 跨域通信的几种方法 */ // jsonp工作原理,参考jsonp.js // 利用hash,场景是当前页面 A 通过iframe或frame嵌入了跨域的页面 B // 在A中伪代码如下: var B = document.getElementsByTagName('iframe'); B.src = B.src + '#' + 'data'; // 在B中的伪代码如下 window.onhashchange = function () { var data = window.location.hash; }; // postMessage // 窗口A(http:A.com)向跨域的窗口B(http:B.com)发送信息 Bwindow.postMessage('data', 'http://B.com'); // 在窗口B中监听 Awindow.addEventListener('message', function (event) { console.log(event.origin); console.log(event.source); console.log(event.data); }, false); // Websocket【参考资料】http://www.ruanyifeng.com/blog/2017/05/websocket.html var ws = new WebSocket('wss://echo.websocket.org'); ws.onopen = function (evt) { console.log('Connection open ...'); ws.send('Hello WebSockets!'); }; ws.onmessage = function (evt) { console.log('Received Message: ', evt.data); ws.close(); }; ws.onclose = function (evt) { console.log('Connection closed.'); }; // CORS【参考资料】http://www.ruanyifeng.com/blog/2016/04/cors.html // url(必选),options(可选) fetch('/some/url/', { method: 'get', }).then(function (response) { }).catch(function (err) { // 出错了,等价于 then 的第二个参数,但这样更好用更直观 }); </script> </body></html>
阅读全文
0 0
- 跨域通信的几种方式(jsonp,hash,postMessage,websocket,cors)
- js跨域总结(jsonp,postMessage,CORS)
- 跨域通信--JSONP和CORS的node版
- nodejs两种方式实现跨域(jsonp,cors)
- 跨域访问的两种解决方式:jsonp和cors
- 跨域请求的三种实现 jsonp,cors,IFame
- 解决跨域的两种方案JSONP和CORS
- jsonp跨域请求的几种方式
- 跨域请求 JSONP & CORS
- 跨域通信的几种方式
- 浅谈Ajax跨域的解决方案——CORS、JSONP
- 跨域请求解决方法(JSONP, CORS)
- jsonp和CORS跨域实现
- JSONP与 CORS 跨域请求
- 跨域请求jsonp与cors
- 跨域请求问题 : jsonp & CORS
- 使用CORS实现跨域的方式
- XMLHttpRequest Level 2的跨域功能(CORS) //另附JSONP的跨域实现
- Codeforces Round #436 (Div. 2) 做题总结
- jQuery事件学习笔记
- 一个机电专业硕士关于未来的思考
- InputStream和OutputStream
- 调试Web应用程序
- 跨域通信的几种方式(jsonp,hash,postMessage,websocket,cors)
- 算法分析与设计课程作业第三周#1
- 开源纯C#工控网关+组态软件
- MATLAB中使用libsvm
- Dungeon Master bfs
- 红黑树插入
- 关于matlab中求定积分int和quad命令的使用
- 上传和引用以编译的module
- 国庆做题报告(一)