Why is it that I can submit a form to an iframe with a cross-domain address but can't send an XMLHTTPRequest to that address?
1 Answer
For the very simple reason that your code doesn't get to see the result of the iframe post. Your script will be denied access to the iframe's DOM.
And keep in mind this is really the browser's security model, not JavaScript's.
2 Comments
MK.
Well, yeah, but wouldn't it make sense to allow XMLHTTPRequest but deny access to the response?
bhamlin
There's no scenario where allowing the XmlHttpRequest would make sense - failing early is better than failing late. Posting to an iframe, however, has plenty of real scenarios (like actually showing the iframe). If you need to make a request and you don't care about the result, consider a hidden image tag.