Javascript Ajax跨子域请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4106993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax cross-sub-domain requests?
提问by Dema
Is there a way to make an Ajax request to
有没有办法向 Ajax 发出请求
s3-ap-southeast-1.s3.amazonaws.com (to S3 API)
s3-ap-southeast-1.s3.amazonaws.com(到 S3 API)
from
从
s3.amazonaws.com
s3.amazonaws.com
(from where a JavaScript app that is hosted at)?
(来自托管 JavaScript 应用程序的位置)?
回答by jwueller
回答by Ricardo
yes, you can cross domain ajax calls, check cross-origin resource sharing: http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
是的,可以跨域ajax调用,查看跨域资源共享:http: //en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing
回答by Shanison
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:
我知道这是一篇旧帖子,我提供了一个使用 JSONP 的跨域 ajax 请求的详细示例,希望它可以帮助那些遇到麻烦的人:
http://www.shanison.com/2012/05/11/cross-domain-ajax-request/
http://www.shanison.com/2012/05/11/cross-domain-ajax-request/
回答by Silver Light
Cross domain ajax requests are forbidden by protocol. And yes, subdomains too.
协议禁止跨域ajax请求。是的,子域也是。
Read here: http://www.ajax-cross-domain.com/It might help;
在这里阅读:http: //www.ajax-cross-domain.com/这可能会有所帮助;
回答by Patrik
shazmo said this in a earlier post:
shazmo 在之前的帖子中说过:
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/
跨域是完全不同的主题。但是跨子域相对容易。
更多信息:http: //www.tomhoppe.com/index.php/2008/03/cross-sub-domain-javascript-ajax-iframe-etc/
回答by Alexandru Nedelcu
Shameless plug, but this may help: http://alexn.org/blog/2011/03/24/cross-domain-requests.html
无耻的插件,但这可能会有所帮助:http: //alexn.org/blog/2011/03/24/cross-domain-requests.html
回答by dav
I guess I found the link that @Patrick had posted and it had become broken
我想我找到了@Patrick 发布的链接,但它已损坏
http://hoppeweb.blogspot.com/2008/03/cross-sub-domain-javascript-ajax-iframe.html
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
.
这个想法是在一个域上设置一个 iframe html,然后从另一个子域上的页面调用该 iframe。父页面和其中的 iframe 都应该具有相同的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".
例如,要提取文本,请在 www.yourdomain.com 上设置页面并将 document.domain 设置为 yourdomain.com。如果您尝试使用来自 img.yourdomain.com 的 Ajax 拉入 html 页面,请设置一个将成为 iframe 的页面来执行 ajax 拉取。拉动完成后,将 document.domain 设置为 yourdomain.com。在您的网页 www. 创建一个 iframe,其中 src 设置为您在 img 上的页面。由于设置了 document.domain,父页面上的任何函数都可以通过 iframe 调用。假设您想将新“ajaxed”的 html 放入父页面上的 div 中,您可以通过“parent.getElementById('yourDivName').innerHTML = Response.Text”来实现。
如果您正在拉入 XML,您可以设置与上述相同的页面/iframe 关系。该 iframe 将对 img.yourdomain.com 上的 XML 进行 ajax 调用并对其进行处理,比如说将其转换为数组。完成后,在 iframe 页面上设置 document.domain。此时,父页面可以通过“iframeName.arrayName”访问其 iframe 上的该数组。或者,您可以在父页面上读取此信息的数组,并通过“parent.arrayName = iframeArray”将其从 iframe 传递给父页面。
originally by @Tom Hoppe
最初由@Tom Hoppe