Google 的 javascript API 如何绕过 AJAX 中的跨域安全

发布于 2024-07-06 05:56:53 字数 50 浏览 7 评论 0 原文

当 Google 的 API 位于您的网站上时,它如何向 Google 发出跨域请求?

How does Google's API make cross-domain requests back to Google, when it's on your website?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

匿名的好友 2024-07-13 05:56:53

他们通过动态地将脚本标签注入文档的头部来解决这个问题。 通过此注入发送的 JavaScript 中有一个回调函数,该函数告诉页面中运行的脚本它已加载以及有效负载(数据)。

然后,该脚本可以删除动态注入的脚本标签并继续。

They get around it by dynamically injecting script tags into the head of the document. The javascript that is sent down via this injection has a callback function in it that tells the script running in the page that it has loaded and the payload (data).

The script can then remove the dynamically injected script tag and continue.

行雁书 2024-07-13 05:56:53

接受的答案是错误的。 本是对的。 下面是使用 Google API JavaScript 客户端

<iframe name="oauth2relay678" id="oauth2relay678" 
        src="https://accounts.google.com/o/oauth2/postmessageRelay?
             parent=https%3A%2F%2Fwww.example.com.au#rpctoken=12345&forcesecure=1" 
             style="width: 1px; height: 1px; position: absolute; left: -100px;">
</iframe>

其工作原理的基本摘要如下:http://ternarylabs .com/2011/03/27/secure-cross-domain-iframe-communication/。 在现代浏览器上,他们利用 HTML postMessage 来实现通信,而在旧浏览器上,他们使用巧妙的 multiple-iframe-urlhash-read+write-combination hack。 Ternary Labs 制作了一个,它抽象出了所有的 hacky 内容,本质上是在所有浏览器上为您提供 postMessage。

有一天,我将在此库的基础上进行构建,以简化跨域 REST API...

编辑:那一天已经到来,XDomain 就在这里 - https://github.com/jpillora/xdomain

The accepted answer is wrong. Ben is correct. Below is the actually iframe node pulled off a page using the Google API JavaScript Client.

<iframe name="oauth2relay678" id="oauth2relay678" 
        src="https://accounts.google.com/o/oauth2/postmessageRelay?
             parent=https%3A%2F%2Fwww.example.com.au#rpctoken=12345&forcesecure=1" 
             style="width: 1px; height: 1px; position: absolute; left: -100px;">
</iframe>

Basic summary of how this works is here: http://ternarylabs.com/2011/03/27/secure-cross-domain-iframe-communication/. On modern browsers they utilize HTML postMessage to achieve communication, and on older browsers, they use a neat multiple-iframe-urlhash-read+write-combination hack. Ternary Labs have made a library which abstracts all the hacky stuff out, essentially giving you postMessage on all browsers.

One day I'll build ontop of this library to simplify cross-domain REST APIs...

Edit: That day has come and XDomain is here - https://github.com/jpillora/xdomain

十级心震 2024-07-13 05:56:53

AFAIK 他们使用 IFRAME。

AFAIK they use IFRAMEs.

骄兵必败 2024-07-13 05:56:53

另一种可能性是使用 dojo 框架中描述的 window.name 传输 这里

Another possibility is to use the window.name transport as described for the dojo framework here

烂柯人 2024-07-13 05:56:53

看起来 Google 使用 显示地图 标签
我猜他们使用 JavaScrit 库来计算 src url 所需的所有坐标和其他参数,然后插入 标签(以及一百万个其他标签)到您的 DOM 中。

完整的地图由几个窗格组成,如下面的 HTML:

(您可以将此 HTML 粘贴到您自己的网页中以查看结果)

因此 Google 地图不使用 AJAX 或任何其他方式来获取其地图,而只是即时创建的纯图像。 所以不用担心跨域问题......

Looks like Google display maps using the <img> tag
I guess they use the JavaScrit library to work out all the co-ordinates and other parameters the src url needs, then insert the <img> tags (along with a million other tags) into your DOM.

The full map is built up with several panes like the HTML below:

<img src="https://mts1.google.com/vt/lyrs=m@248102691&hl=en&src=app&x=32741&s=&y=21991&z=16&scale=1.100000023841858&s=Galile" class="css-3d-layer" style="position: absolute; left: 573px; top: 266px; width: 128px; height: 128px; border: 0px; padding: 0px; margin: 0px;">

(You can paste this HTML into your own web page to see the result)

So Google Maps does NOT use AJAX or anything to get its maps, just plain images, created on the fly. So no Cross Domain issues to worry about...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文