同源策略 - AJAX 和 AJAX使用公共 API
我知道在我自己的网页上,我的用户是否在: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决同源策略的已知方法很少。一种流行的技术是使用“脚本标签注入”,例如 JSONP 中的技术。由于
标签不受同源策略的约束,第三方域上的脚本可以提供与所提供的回调函数交互的可执行代码。您可能需要查看以下文章中的“提示和技巧”部分,以进一步阅读该主题:
您可能还有兴趣查看以下 Stack Overflow 帖子,以进一步阅读有关解决同源策略的其他技术:
更新:进一步更新问题:
引用
$.getJSON()
上的 jQuery 文档: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()
: