How to get data from an external site inside a cor

2019-06-06 06:18发布

Scenario:

I've an application made in angularJS and ionic for cordova 3.5
This application loads trough an iframe a web to make some things with a step by step form. This web is on other site.

The code for the html is:

<div id="IframeContainer">  

 <iframe src="URL" style="width:100%;height:90%" onLoad="checkforclose(this);"></iframe>

</div>

This step-by-step form returns a result that the cordova application needs to know what happens in the form. It can return a json, a text/plain or even an HTML that auto-post to another site (This is linked with this non-answered question: Post and redirect FROM Web Api)

Said this, in my cordova application I've a javascript function in order to close the iframe and take over again the control of my application, detecting if the url contains the word "close". This is the code:

<script type="text/javascript">
function checkforclose(pageURL) { 
    var urlFrame = pageURL.contentWindow.location;
    if (urlFrame.href.indexOf('close') > -1) {
    window.location = "#/employees/";
        }
}   
</script>

Question:

Trying avoid CORS (So I think I can't read the iframe content on load, or I'm wrong?),
without using jQuery (AngularJS is welcome, plain javascript even more)
Taking over the control again to the application

How can I get the data returned by the step-by-step external form?


UPDATE 1:
I tried coding a "onload" reading (CORS errors), and posting to a cordova-html page, but without any respectable result.

1条回答
Juvenile、少年°
2楼-- · 2019-06-06 06:26

A possible solution is Web messaging or cross-document messaging. Here's a blog post where someone used this method to gain access to a mobile device's camera from an external page loaded in an iframe. Although this person had the opposite goal (get data from Cordova to page loaded in iframe), they were able to accomplish cross domain communication between a page in an iframe and Cordova; which is what I believe you are trying to do.

查看更多
登录 后发表回答