Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cross-domain AJAX - would this method work?

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 asynchronicity?)

like image 442
wheresrhys Avatar asked Mar 06 '10 10:03

wheresrhys


People also ask

Can you do cross domain AJAX?

For a successful cross-domain communication, we need to use dataType “jsonp” in jquery ajax call. JSONP or “JSON with padding” is a complement to the base JSON data format which provides a method to request data from a server in a different domain, something prohibited by typical web browsers.

How do I create a cross origin AJAX request?

For instance, if sending a request from http://www.example.com, any of the following would be "cross origin": http://mail.example.com (domain differs) https://www.example.com (protocol differs) http://www.example.com:8080 (port differs)

When working with AJAX applications which is faster?

Parsing JSON was 10 times faster than parsing XML! When it comes to making AJAX behave like a desktop application, speed is everything, and clearly, JSON is a winner. Of course, you might not always have control of the server-side that's producing data for your AJAX application.


2 Answers

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

like image 85
Peter Avatar answered Sep 30 '22 11:09

Peter


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,

like image 35
Andreas Avatar answered Sep 30 '22 11:09

Andreas