1

我对与跨域相关的两件事有点困惑。

假设我有 2 个域;mydomain.com 和 otherdomain.com

现在在 mydomain.com 上,可以从 otherdomain.com 访问的所有内容是什么?我的意思是我们可以有

<img src="otherdomain.com/xyz.jpg">

同样,我们可以在 iframe src 中直接使用 otherdomain.com 吗?默认情况下允许什么?可以做些什么来阻止 otherdomain.com 的用户访问?

第二部分与 JavaScript/AJAX 有关。在脚本相关的东西中默认阻止其他域的东西吗?使用 AJAX,我可以默认向 otherdomain.com 发出请求吗?是否允许?如果不允许,如何从 otherdomain.com 获得响应?

非常感谢。

4

3 回答 3

0

Read Wikipedia.

You cannot read from another domain (unless it allows you to).

You can display or execute content from another domain (eg, using an image, frame, or script tag), but you can't read it directly from your code.
Thus, you cannot send an AJAX request to another domain, and you cannot read the contents of an image, frame, or script tag that was loaded from another domain.

于 2013-03-08T13:56:02.790 回答
0

可以从 otherdomain.com 访问的所有内容是什么?我的意思是我们可以有<img src="otherdomain.com/xyz.jpg">

您需要区分“显示”和“访问”。您可以包含图像,但由于同源策略 (SOP)的原因,您无法访问它的数据。

同样我们可以在iframe src中直接使用otherdomain.com吗?默认情况下允许什么?

您可以包含可以链接的所有内容,从样式表、脚本、图像到通过框架的整个页面。从其他域执行脚本实际上是一种获取数据的标准方法,称为JSONP;并且包括来自第三方 CDN 的资源也很常见。

可以采取哪些措施来阻止 otherdomain.com 的用户访问?

您可以使用X-FRAME-OPTIONS-header来防止通过框架包含,大多数浏览器都应该尊重这一点。

您可以尝试避免使用错误的REFERER标头回答请求(发送 404 内容),但这不是一种可靠的方法,因为 REFERER 经常被浏览器禁用或被防火墙阻止。

第二部分与 JavaScript/AJAX 有关。在脚本相关的东西中默认阻止其他域的东西吗?使用 AJAX,我可以默认向 otherdomain.com 发出请求吗?是否允许?

不,对数据的访问被阻止。您可以发送请求,但除非发送CORS 标头以明确允许,否则您的脚本将无法获得响应。

如果不允许,如何从 otherdomain.com 获得响应?

您可以在 mydomain.com 上使用代理。

于 2013-03-08T14:05:25.867 回答
0

我们能有吗<img src="otherdomain.com/xyz.jpg">

是的,我们可以拥有这个和任何其他资源,如图像、视频和音频文件、zip、pdf ......

我们可以直接在 iframe src 中使用 otherdomain.com 吗?

我可以默认向 otherdomain.com 发出请求吗?是否允许?

不,出于安全原因

如果不允许,如何从 otherdomain.com 获得响应?

如果你拥有 otherdomain.com,你可以使用 jsonp 和一些 php 的东西。

http://remysharp.com/2007/10/08/what-is-jsonp/
于 2013-03-08T14:09:19.713 回答