一个 jQuery 实例,两个域
我有两个页面: a.example.com 和 b.example.com
a.example.com 包含 jQuery
a.example .com 包含一个指向 b.example.com 的 iframe,
两个页面都将 document.domain 设置为相同的父域,example.com
我如何使用jQuery include 从 a.example.com 调用$.ajax({ url: "b.example.com" }) 来自 b.example.com iframe 内部?
换句话说:两个页面当前都可以访问彼此的 Javascript,但我无法在不引发 XSS 错误的情况下获得对函数的 AJAX 调用。也就是说,b.example.com 上也不包含 jQuery。如何避免两次包含 jQuery?
iframe 内容示例:
<script>
document.domain = "example.com";
function proxyAjax() {
var jQueryParent = parent.$.sub();
// Chrome gives error: XMLHttpRequest cannot load http://b.example.com/. Origin http://a.example.com/ is not allowed by Access-Control-Allow-Origin.
jQueryParent.ajax({
url : "http://b.example.com/",
success : function() {
console.debug("Success");
}
});
}
proxyAjax();
</script>
I have two pages: a.example.com and b.example.com
a.example.com includes jQuery
a.example.com contains an iframe pointing to b.example.com
both pages have document.domain set to the same parent domain, example.com
How can I use the jQuery include from a.example.com to call $.ajax({ url: "b.example.com" }) from inside the b.example.com iframe?
In other words: Both pages can currently access the Javascript of one another, but I can not get the AJAX call to function without throwing XSS errors. That is, without including jQuery on b.example.com too. How do I avoid including jQuery twice?
Example of the contents of the iframe:
<script>
document.domain = "example.com";
function proxyAjax() {
var jQueryParent = parent.$.sub();
// Chrome gives error: XMLHttpRequest cannot load http://b.example.com/. Origin http://a.example.com/ is not allowed by Access-Control-Allow-Origin.
jQueryParent.ajax({
url : "http://b.example.com/",
success : function() {
console.debug("Success");
}
});
}
proxyAjax();
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
设置
document.domain
将不允许您进行跨域 AJAX 调用。如果您确实不需要来自 b.example.com 的请求,您可以执行以下操作:在 a.example.com 文档中,使用此 JavaScript 函数:
然后在 b.example.com 文档中,使用一个脚本使用 b.example.com 中定义的回调函数调用 a.example.com 文档中的
makeAjaxRequest
。Setting
document.domain
will not allow you to make cross-domain AJAX calls. If you don't really need the request to originate from b.example.com, you could do this:In the a.example.com document, have this JavaScript function:
Then in the b.example.com document, have a script that calls
makeAjaxRequest
in the a.example.com document with a callback function defined in b.example.com.