title: How to implement iFrame communication to prevent from cross origin error?
body: <p>I am trying to send a http request to a third party API through an iframe to overcome the issue with cross origin error "origin not found in access-control-allow-origin"</p>
  
  <p>The third party API does not return Origin in the response header and they don't accept JSONP http request either so I am trying to implement this using iFrame. 
  I am testing simple html forms to see if it works. Below I have Parent and Child html. My web application owns both Parent and Child. The parent sends a message to child
  using postMessage and then child makes a GET request to the specified url and tries to post the response to parent.</p>
  
  <p>When I run this, I see the response in network tab from the third party but the browser is still preventing to send it to the iFrame (child.html). Can anyone point out what I am missing here?
  I think I need to target the iFrame to be same domain as the third party.
  Thank you for any advice.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
   &lt;meta charset="utf-8"&gt;
   &lt;title&gt;postMessage Demo: Parent&lt;/title&gt;
  
      &lt;script type="text/javascript"&gt;
          window.onload = function () {
              var receiver = document.getElementById("ifrmChild").contentWindow;
  
              var btn = document.getElementById('send');
  
              function sendMessage(e) {
                  e.preventDefault();
                  receiver.postMessage('https://test.computing.com/my-pay/MyPay.do?UserID=8888&amp;Amount=100&amp;Name=Test', '*');
              }
  
              btn.addEventListener('click', sendMessage);
             
              function ReceiveMessage(evt) {
                  alert(evt.data);
              }
     
     window.addEventListener("message", ReceiveMessage, false);
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
      &lt;iframe id="ifrmChild" src="receiver.html" frameborder="0" width="500" height="200"&gt;
      &lt;/iframe&gt;
      &lt;br /&gt;
  
      &lt;p&gt;
          &lt;button id="send"&gt;Send Message&lt;/button&gt;
      &lt;/p&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
   &lt;meta charset="utf-8"&gt;
   &lt;title&gt;Child&lt;/title&gt;
      &lt;script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"&gt;&lt;/script&gt;
  
      &lt;script type="text/javascript"&gt;
  
          window.onload = function () {
  
              var messageEle = document.getElementById('message');
  
              function receiveMessage(evt) {
                  messageEle.innerHTML = "Message Received: " + evt.data;
  
                      $.ajax({
                          url: evt.data,
                          type: 'GET',
                          success: function (data, textStatus, jqXHR) {
                              console.log(data);
                              evt.source.postMessage(data, "*");
                          },
                          error: function (jqXHR, textStatus, errorThrown) {
                              console.log(textStatus + " " + errorThrown);
                              evt.source.postMessage(textStatus, "*");
                          }
                      });
              }
  
              // Setup an event listener that calls receiveMessage() when the window
              // receives a new MessageEvent.
              window.addEventListener('message', receiveMessage);
          }
   &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body style="background-color: gray;"&gt;
      &lt;h1&gt;Receiver&lt;/h1&gt;
      &lt;div id="message"&gt;&lt;/div&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>