Dema
Dema

Reputation: 81

Ajax cross-sub-domain requests?

Is there a way to make an Ajax request to

s3-ap-southeast-1.s3.amazonaws.com (to S3 API)

from

s3.amazonaws.com

(from where a JavaScript app that is hosted at)?

Upvotes: 8

Views: 21114

Answers (7)

dav
dav

Reputation: 9267

I guess I found the link that @Patrick had posted and it had become broken

http://hoppeweb.blogspot.com/2008/03/cross-sub-domain-javascript-ajax-iframe.html

to avoid happening this again I will just try to re-post it)

The idea is setting up an iframe html on one domain and then calling that iframe from the page on the other subdomain. Both parent page and the iframe inside it should have the same document.domain.

document.domain = "example.com"

once done, those two pages act like they are on the same domain.

the rest, just copy-pasted ((

For example, for pulling in text, setup your page on www.yourdomain.com and set document.domain to yourdomain.com. If you are trying to pull in an html page using Ajax from img.yourdomain.com, setup a page that, will become the iframe, to do the ajax pull. After that pull is complete set the document.domain to yourdomain.com. In your page on www. create an iframe which has the src set to your page on img. Since document.domain is set, any functions on the parent page are available to be called via the iframe. Lets say you want to put your newly "ajaxed" html into a div on the parent page, you can do that via "parent.getElementById('yourDivName').innerHTML = Response.Text".

If you are pulling in XML, you can setup the page/iframe relationship the same as above. That iframe will make the ajax call to the XML on img.yourdomain.com and do something with it, lets say turn it into an array. Once that is completed, set the document.domain on the iframe page. At this point, the parent page can access that array on its iframe via "iframeName.arrayName". Alternatively you can have an array read on the parent page for this information and pass it to the parent from the iframe via "parent.arrayName = iframeArray".

originally by @Tom Hoppe

Upvotes: 0

Shanison
Shanison

Reputation: 2295

I know this is an old post, I provided a detailed example for cross domain ajax request using JSONP, hopefully it helps those who is in trouble:

http://www.shanison.com/2012/05/11/cross-domain-ajax-request/

Upvotes: 1

Ricardo
Ricardo

Reputation: 1411

yes, you can cross domain ajax calls, check cross-origin resource sharing: http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

Upvotes: 3

Alexandru Nedelcu
Alexandru Nedelcu

Reputation: 8069

Shameless plug, but this may help: http://alexn.org/blog/2011/03/24/cross-domain-requests.html

Upvotes: 0

Patrik
Patrik

Reputation: 479

shazmo said this in a earlier post:

Cross domain is entirely a different subject. But cross sub-domain is relatively easy.

More info here: http://www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/

Upvotes: 0

jwueller
jwueller

Reputation: 30996

You cannot do cross-domain ajax requests. That includes subdomains. However, it is possible to use JSONP.

Upvotes: 3

Silver Light
Silver Light

Reputation: 45922

Cross domain ajax requests are forbidden by protocol. And yes, subdomains too.

Read here: http://www.ajax-cross-domain.com/ It might help;

Upvotes: 1

Related Questions