/ Published in: JavaScript
Usage:
https://goodguys.com
You should never see this
than just postMessage to an iframe with this HTML + JS, and wait for the reply. iframe will do request for you
https://goodguys.com
You should never see this
than just postMessage to an iframe with this HTML + JS, and wait for the reply. iframe will do request for you
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function checkOrigin(origin) { var allows = $('.allow-origin'); for (var i = 0; i < allows.length; i++) { var allow = $(allows[i]).text(); if (allow == "*") return true; if (allow == origin) return true; } return false; } function messageHandler(e) { var origin = e.originalEvent.origin; if (!checkOrigin(origin)) return; var request = JSON.parse(e.originalEvent.data); // TODO remove this IF after testing if (origin == "file:" || origin == "null") origin = "*"; $.ajax(request.ajax) .done(function (data) { request.result = data; var reply = JSON.stringify(request); parent.postMessage(reply, origin); }) .fail(function (req) { request.error = req.statusText; request.errorCode = req.status; var reply = JSON.stringify(request); parent.postMessage(reply, origin); }); } $(document).on('ready', function () { $(window).on('message', messageHandler); });