views:

166

answers:

3

I've been thinking for a while if there's a way to get cross-domain ajax (using XML, not JSON) to work. Can anyone see any reason why the following wouldn't work:

  1. Create an iframe
  2. Load the url of the ajax call in the iframe
  3. Use javascript to get the content of the iframe

(this won't be asynchronous, but could using setTimeout(functionToLoadTheIframe, 1) enable you to achieve asynchronisity?)

A: 

This would not work due to same domain security restrictions. In most cases you can not interact with frames that have a different domain. Otherwise a malicious site could load your email inbox for example.

Gdeglin
+1  A: 

iframes where used to allow remote calls and "simulate" ajax as we know it today, and the answer is YES...you can use iframes but you have to write sometimes complex client side script using javaScript.

Have you considered using a server side script/application on your domain as a proxy to the remote resource?

That is, send your requests to your script/application on your domain which in turn will fetch data/information from the remote host.

regards,

andreas
I'm using a server side script at the moment and it's working fine, so my question is partly for curiosity value, but partly to see if there's a workaround in future if I want to use an XML webservice in a plain html page using javascript but no server-side scripting.
wheresrhys
it is doable with iframes with restrictions..so you should look into it first before implementing.regards.
andreas
+1  A: 

Yes you can use iframes for cross domain AJAX. It get's a little complicated (more so if you want to use XML), but it is possible. Here's a good article that describes several different hacks that are used. I think one is pretty similar to what you're describing. More importantly he describes the rules on iframe to iframe communication which will probably help the most.

http://softwareas.com/cross-domain-communication-with-iframes

Peter
Really useful link - cheers
wheresrhys