开发者

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


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


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


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

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


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/


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/


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜