The Hidden Limitation of Posting to Iframes via jQuery
Using jQuery's $.post directly to an iframe is a common stumbling block because the standard AJAX request hijacks the default form submission behavior, effectively severing the link between the sending document and the receiving frame. When you load an iframe with a simple <iframe src="...">, the browser navigates the window, executes scripts in the new context, and allows that context to interact with window.parent if same-origin policies are satisfied. This works perfectly for GET requests, as demonstrated by creating a hidden temporary iframe, appending it to the body, and letting the server inject JavaScript before removing it.
The core difficulty arises when attempting to push data using POST while maintaining the ability for the server to flush buffers and inject scripts back into the parent window. A standard $.post call does not navigate the current document; instead, it sends an XHR request in the background. If your PHP backend relies on ob_flush() or flush() to output JavaScript immediately upon receiving POST data, that script never executes because the iframe's main thread isn't waiting for a navigation eventâit is simply sitting idle.
To replicate the success of your GET-based solution with POST data, you must bypass jQuery's AJAX interception and force a full page load within the iframe's document object. The most reliable method involves setting the src attribute directly to a URL that accepts POST parameters via standard query strings or form submissions, rather than relying on an invisible AJAX call.
However, there is a critical architectural constraint you must address: cross-origin restrictions. If your main page and the server generating the iframe content reside on different domains, the browser's Same-Origin Policy will block window.parent access from within the iframe, rendering any injected JavaScript useless regardless of how you load it. The source material implies a same-origin scenario (likely a PHP backend serving both views), which is necessary for this pattern to work at all.
If you need to send POST data specifically through query parameters to maintain the buffer-flushing workflow, you can construct a URL that accepts POST variables in the GET request string (e.g., server.php?type=getFolders&inode=123). While semantically incorrect for RESTful APIs, this technique allows PHP to read $_GET['type'] and $_GET['inode'], execute your logic, flush the buffers, and inject the script. The downside is that sensitive data exposed in URLs should be avoided.
Alternatively, if you must use a genuine POST payload (JSON or form data), the iframe's src needs to point to a URL that accepts POST requests. You cannot do this with jQuery's $.post. Instead, create a hidden <form> element within the iframe's document or simply navigate the iframe to a new URL after constructing it with your parameters.
The implementation logic remains consistent with your GET example:
- Create a hidden iframe element.
- Append it to the DOM.
- Set the
srcattribute to point to your server endpoint, ensuring the URL accepts POST data if necessary. - Listen for the
loadevent to determine when the script has been injected and executed. - Clean up by removing the iframe once the operation completes.
var iframe = $('<iframe style="display:none;" />');
$('body').append(iframe);
// Ensure the src accepts POST or matches the GET pattern if using query params
iframe.attr('src', 'server.php?type=getFolders&inode=' + nodeData.inode);
iframe.load(function () {
// The script injected by PHP has executed here.
// You can now interact with window.parent methods safely.
console.log('Data posted and processed');
iframe.remove(); // Clean up immediately after success
});
This approach forces the browser to treat the request as a navigation, triggering the execution of any buffered JavaScript output from the server. It preserves the ability for the server-side code to manipulate the parent window context, which is the specific requirement that standard AJAX calls cannot fulfill without complex cross-origin messaging workarounds like postMessage.