CORS 对跨域消息传递的好处 [英] Benefit of CORS over cross-domain messaging

查看:26
本文介绍了CORS 对跨域消息传递的好处的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CORS 和跨域消息传递在我看来是一样的:它们允许跨域通信.

CORS and cross-domain messaging look the same to me: they allow communication across domains.

有什么理由使用一个与另一个?

Are there any reasons to use one vs. the other?

推荐答案

CORS 适用于 Flash 通常不允许的 ajax 请求或 flash 请求.例如,如果域 x 没有跨域策略,并且您通过 flash 从那里检索 mp3 文件进行播放,则 flash 将不允许您读取 mp3 文件的 id3 标签.对于 ajax,如果目标服务器没有允许您的域发出请求的跨域策略,则您完全无法发出请求.

CORS is for ajax requests or flash requests that flash wouldn't normally allow. For example, if there is no cross-domain policy for domain x, and you retrieve an mp3 file from there via flash for playback, flash will not allow you to read the id3 tags of the mp3 file. For ajax, you flat out cannot make the request if the target server doesn't have a cross-domain policy that allows your domain to make requests.

跨域消息传递允许您与文档中来自不同来源的 iframe 进行通信.例如,如果您有 youtube 视频 iframe,您可以向该 iframe 传递一条消息以更改音量.通常不可能进行通信,因为 iframe 具有不同的来源,因此您无法以编程方式对 youtube iframe 执行任何操作.

Cross-domain messaging allows you to communicate with an iframe in the document that is from different origin. For example, if you have youtube video iframe, you may pass a message to that iframe to change volume. Normally no communication wouldn't be possible because the iframe has a different origin, so you could not do anything with the youtube iframe programmatically.

现在应该清楚使用一个或另一个的原因.CORS 允许您从另一个来源请求数据,而当您想与 iframe 内但不在同一来源的应用程序通信时,使用主窗口和 iframe 之间的消息传递.

The reasons to use one or another, should be now clear. CORS allows you to request data from another origin while message passing between main window and an iframe is used when you want to communicate with an app that is inside the iframe but is not in the same origin.

一个实际的例子:

1.您有一个带有 youtube 播放器的 iframe.

1.You have an iframe that has a youtube player.

2.您请求从 youtube 数据 api 播放一些视频(CORS,可以是 JSONP、XHR 或其他).

2.You request some videos to play from youtube data api (CORS, could be JSONP, XHR or whatever).

3.您现在将跨域消息传递给 iframe 以开始播放您在第 2 步中请求的任何视频

3.You now pass a cross-domain message to the iframe to start playing any of the video you requested in step #2

这篇关于CORS 对跨域消息传递的好处的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆