同源策略 - AJAX 和 AJAX使用公共 API

发布于 2024-09-15 13:20:43 字数 750 浏览 15 评论 0原文

我知道在我自己的网页上,我的用户是否在: http://www.example.com/form.php

我做了一个ajax 请求从该页面发送到: http://example.com/responder.php

由于同源,会失败策略(子域不同)。

我想了解的是,当请求和服务器明显不同时,AJAX 请求如何从像 flickr 这样的 API 中提取数据。


编辑:

例如:为什么这段代码有效?

$.getJSON('http://api.flickr.com/services/rest/?&;method=flickr...'

引用了此社区 Wiki) 是否使用跨源资源共享

谢谢!

I know if on my own webpage, if my user is on :
http://www.example.com/form.php

and I make an ajax request from that page to :
http://example.com/responder.php

It will fail because of the Same origin policy (subdomain is different).

What I am trying to understand is, how is it that AJAX requests can pull data from API's like flickr when the request and server are obviously different.


Edit :

eg: Why does this code work?

$.getJSON('http://api.flickr.com/services/rest/?&;method=flickr...'

(Referred this Community Wiki)
Is it using Cross Origin Resource Sharing?

Thanks!

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

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

发布评论

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

评论(1

如日中天 2024-09-22 13:20:43

解决同源策略的已知方法很少。一种流行的技术是使用“脚本标签注入”,例如 JSONP 中的技术。由于

您可能还有兴趣查看以下 Stack Overflow 帖子,以进一步阅读有关解决同源策略的其他技术:


更新:进一步更新问题:

引用 $.getJSON() 上的 jQuery 文档:

如果 URL 包含字符串“callback=?”在 URL 中,请求被视为 JSONP。

There are few known methods to work around the Same Origin Policy. One popular technique is to use "Script Tag Injection" such as in JSONP. Since the <script> tag is not constrained by the Same Origin Policy, a script on a third-party domain can provide executable code that interacts with a provided callback function. You may want to check out the "Tips and Tricks" section in the following article for further reading on the topic:

You may also be interested in checking out the following Stack Overflow post for further reading on other techniques to work around the Same Origin Policy:


UPDATE: Further the updated question:

Quoting from the jQuery documentation on $.getJSON():

If the URL includes the string "callback=?" in the URL, the request is treated as JSONP instead.

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