Access Control Allow Origin simulation for IE8 and IE9


/ Published in: JavaScript
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. function checkOrigin(origin) {
  2. var allows = $('.allow-origin');
  3. for (var i = 0; i < allows.length; i++) {
  4. var allow = $(allows[i]).text();
  5. if (allow == "*")
  6. return true;
  7. if (allow == origin)
  8. return true;
  9. }
  10. return false;
  11. }
  12.  
  13. function messageHandler(e) {
  14. var origin = e.originalEvent.origin;
  15. if (!checkOrigin(origin))
  16. return;
  17.  
  18. var request = JSON.parse(e.originalEvent.data);
  19.  
  20. // TODO remove this IF after testing
  21. if (origin == "file:" || origin == "null")
  22. origin = "*";
  23.  
  24. $.ajax(request.ajax)
  25. .done(function (data) {
  26. request.result = data;
  27. var reply = JSON.stringify(request);
  28. parent.postMessage(reply, origin);
  29. })
  30. .fail(function (req) {
  31. request.error = req.statusText;
  32. request.errorCode = req.status;
  33. var reply = JSON.stringify(request);
  34. parent.postMessage(reply, origin);
  35. });
  36. }
  37.  
  38. $(document).on('ready', function () {
  39. $(window).on('message', messageHandler);
  40. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.