JSON 和 JSONP 有什么区别?

发布于 2024-09-05 20:46:44 字数 286 浏览 0 评论 0 原文

可能的重复:
请解释一下 JSONP

例如,在 jQuery 文档中,我发现 JSON 和 JSONP 都提到了。到底有什么区别呢?我怎样才能看到哪个是哪个?哪一个应该用来做什么?

PHP 函数 json_encode 生成什么?

Possible Duplicate:
Please explain JSONP

For example in the jQuery documentation I find both JSON and JSONP mentioned. What is the difference exactly? How can I see which is which? Which one should be used for what?

And what does the PHP function json_encode generate?

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

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

发布评论

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

评论(1

倒带 2024-09-12 20:46:44

JSON 是一种简单的数据格式。 JSONP 是一种跨域使用该格式的方法 ajax 请求,同时未被 同源政策问题。基本上,这个想法是,您不使用 ajax 请求 JSON 编码的数据,而是向页面添加一个脚本标记,该标记将数据作为 JavaScript 脚本加载,并对您的代码进行回调,显示“这是数据”。这是有效的,因为应用于 JavaScript 脚本的“来源”是文档的来源,而不是脚本的来源,这意味着它可以访问您的代码以调用回调。

json_encode 生成 JSON。如果您需要启用跨域调用,您可以使用 json_encode 作为向系统提供 JSONP 接口的一部分。

另请参阅 CORS,随着我们的发展,它可能会越来越多地用于此目的,但是IE 中还没有很好地支持(IE7 及以下版本根本没有;IE8 有,但要求客户端代码做特殊的事情;Chrome、Firefox 等有它,但不需要执行任何特殊操作的客户端代码)。

JSON is a simple data format. JSONP is a methodology for using that format with cross-domain ajax requests while not being hit by Same Origin Policy issues. Basically, the idea is that instead of using ajax to request JSON-encoded data, you add a script tag to your page that loads the data as a JavaScript script and makes a callback to your code saying "Here's the data." This works because the "origin" applied to JavaScript scripts is the origin of the document, not where the script came from, which means it can access your code in order to call the callback.

json_encode produces JSON. You might use json_encode as part of providing a JSONP interface to your system, if you need to enable cross-domain calls.

See also CORS, which may increasingly be used for this instead as we go forward, but which isn't yet supported well in IE (IE7 and below don't have it at all; IE8 has it but requires that the client-side code do special things; Chrome, Firefox, and the like have it and don't require the client-side code to do anything special).

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