JSONP解释及使用
可能的重复:
请解释一下 JSONP
什么是 JSONP,为什么需要它,以及它的一些实际用法示例。
Possible Duplicate:
Please explain JSONP
What is JSONP, why is it needed, and what are some real world examples of it's usage.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSONP 代表带有填充的 JSON,它为客户端提供了一种方法来指定应添加到 JSON 响应开头的一些代码。这允许 JSONP 响应直接在浏览器中执行。 JSONP 响应的一个示例可能是:
我认为 JSONP 有用的主要地方是使用
标签跨域发出请求。我认为主要缺点是,由于 JSONP 是直接执行的,因此您必须相信远程站点不会发回任何恶意内容。但我必须承认我还没有在实践中使用过该技术。
编辑:远程 JSON - JSONP 提供了有关的更多信息为什么你想要使用似乎发明了该技术的人的技术。
JSONP stands for JSON with padding, and it provides a way for the client to specify some code that should be added to the start of the JSON response. This allows the JSONP response to be directly executed in the browser. An example of a JSONP response might be:
I think the major place that JSONP would be useful is in making requests across domains using the
<script>
tag. I think the major disadvantage is that as the JSONP is directly executed you would have to trust that the remote site wouldn't send back anything malicious. However I have to admit that I haven't used the technique in practice.Edit: Remote JSON - JSONP provides more information on why you would want to use the technique from the guy who appears to have invented it.
http://remysharp.com/2007/10/08/what-is -jsonp/
http://remysharp.com/2007/10/08/what-is-jsonp/
引用自此博客:
JSONP 的目的是克服臭名昭著的同域策略的边界,该策略将 XHR 请求限制到同一域,这意味着您无法向其他域发出 Ajax 请求。使用 JSONP 无需担心这个问题,因为它甚至不需要 Ajax 即可工作;它所做的只是使用脚本标签和回调......
Quoting from this blog:
The purpose of JSONP is to overcome the boundaries of the infamous same-domain-policy which restricts XHR requests to the same domain meaning that you cannot make Ajax requests to other domains. There’s no need to worry about that with JSONP because it doesn’t even require Ajax to work; all it’s doing is using script tags and callbacks…