I have a Same-Origin Policy error when trying to fetch the URL of the webpage displayed by an iframe

41 Views Asked by At

I want to fetch the URL of a webapp page that I view inside an iframe.

The webapp runs on tomcat. All tomcat files are located in the "/opt/open720" folder.

The application containing the iframe and the webapp that must be displayed through the iframe run on the same vitual machine but on different ports (3000 and 5443 respectively).

I added the following code to the "/opt/open720/conf/web.xml" file

<filter>
    <filter-name>CorsFilter</filter-name>
    <filter-class>org.jboss.resteasy.plugins.interceptors.CorsFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>CorsFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

I put the "cors-filter-2.4.jar" in the "/opt/open720/lib" directory

I restarted tomcat

sudo service tomcat4 restart

I ran the following code

const iframe = document.getElementById('iframe');
const iframeURL = iframe.contentWindow.location.href;
console.log('URL of the page within the iframe:', iframeURL);

But I still have this error

Uncaught DOMException: Failed to read a named property 'href' from 'Location': Blocked a frame with origin "https://<virtual machine ip>:3000" from accessing a cross-origin frame.
    at https://<virtual machine ip>:3000/iframe:23:57
0

There are 0 best solutions below