This error occurs because of the Same-Origin Policy, which restricts access to resources from a different origin. In this case, the form submission is trying to access a property on a cross-origin object, which is not allowed.
To fix this, you can use the postMessage API to communicate between the iframe and the parent window. Here's an example of how to do this:
In the parent window:
<template>
<div>
<iframe ref="myIframe" src="https://example.com"></iframe>
</div>
</template>
<script>
export default {
mounted() {
window.addEventListener('message', this.handleMessage)
},
methods: {
handleMessage(event) {
if (event.origin !== 'https://example.com') {
return
}
// Handle message from iframe
}
}
}
</script>
In the iframe:
<template>
<div>
<form @submit.prevent="submitForm">
<!-- Form fields -->
<button type="submit">Submit</button>
</form>
</div>
</template>
<script>
export default {
methods: {
submitForm() {
// Send message to parent window
window.parent.postMessage({ type: 'formSubmitted' }, 'https://parent-window.com')
}
}
}
</script>
This code sets up a message listener in the parent window that listens for messages from the iframe. When the form is submitted in the iframe, it sends a message to the parent window with the type "formSubmitted". You can then handle this message in the parent window and perform any necessary actions.