`
Joson_Coney
  • 浏览: 54711 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Remote JSON - JSONP

 
阅读更多

( From : http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/ )

Remote JSON - JSONP

December 05, 2005 at 08:21 PM | categories: AJAXjavascriptMochiKit | 15 Comments

 

The browser security model dictates that XMLHttpRequest, frames, etc. must have the same domain in order to communicate. That's not a terrible idea, for security reasons, but it sure does make distributed (service oriented, mash-up, whatever it's called this week) web development suck.

There are traditionally three solutions to solving this problem.

Local proxy:
Needs infrastructure (can't run a serverless client) and you get double-taxed on bandwidth and latency (remote - proxy - client).
Flash:
Remote host needs to deploy a crossdomain.xml file, Flash is relatively proprietary and opaque to use, requires learning a one-off moving target programming langage.
Script tag:
Difficult to know when the content is available, no standard methodology, can be considered a "security risk".

I'm proposing a new technology agnostic standard methodology for the script tag method for cross-domain data fetching: JSON with Padding, or simply JSONP.

The way JSONP works is simple, but requires a little bit of server-side cooperation. Basically, the idea is that you let the client decide on a small chunk of arbitrary text to prepend to the JSON document, and you wrap it in parentheses to create a valid JavaScript document (and possibly a valid function call).

The client decides on the arbitrary prepended text by using a query argument named jsonp with the text to prepend. Simple! With an empty jsonp argument, the result document is simply JSON wrapped in parentheses.

Let's take the del.icio.us JSON API as an example. This API has a "script tag" variant that looks like this:

http://del.icio.us/feeds/json/bob/mochikit+interpreter:

if(typeof(Delicious) == 'undefined') Delicious = {}; Delicious.posts = [{ "u": "http://mochikit.com/examples/interpreter/index.html", "d": "Interpreter - JavaScript Interactive Interpreter", "t": [ "mochikit","webdev","tool","tools", "javascript","interactive","interpreter","repl" ] }]

In terms of JSONP, a document semantically identical to this would be available at the following URL:

http://del.icio.us/feeds/json/bob/mochikit+interpreter?jsonp=if(typeof(Delicious)=='undefined')Delicious={};Delicious.posts=

That's not very interesting on its own, but let's say I wanted to be notified when the document is available. I could come up with a little system for tracking them:

var delicious_callbacks = {}; function getDelicious(callback, url) { var uid = (new Date()).getTime(); delicious_callbacks[uid] = function () { delete delicious_callbacks[uid]; callback(); }; url += "?jsonp=" + encodeURIComponent("delicious_callbacks[" + uid + "]"); // add the script tag to the document, cross fingers }; getDelicious(doSomething, "http://del.icio.us/feeds/json/bob/mochikit+interpreter");

The fetched URL from this hypothetical experiment would look something like this:

http://del.icio.us/feeds/json/bob/mochikit+interpreter?jsonp=delicious_callbacks[12345]

delicious_callbacks[12345]([{ "u": "http://mochikit.com/examples/interpreter/index.html", "d": "Interpreter - JavaScript Interactive Interpreter", "t": [ "mochikit","webdev","tool","tools", "javascript","interactive","interpreter","repl" ] }])

See, because we're wrapping with parentheses, a JSONP request can translate into a function call or a plain old JSON literal. All the server needs to do differently is prepend a little bit of text to the beginning and wrap the JSON in parentheses!

Now, of course, you'd have libraries like MochiKitDojo, etc. abstracting JSONP so that you don't have to write the ugly DOM script tag insertion yourself, etc.

Of course, this just solves the standardization problem. Your page is still toast if the remote host decides to inject malicious code instead of JSON data. However, if implemented, it'd save a lot of developers some time and allow for generic abstractions, tutorials, and documentation to be built.


分享到:
评论

相关推荐

    json-viewer, 它是用于打印JSON和JSONP的Chrome 扩展.zip

    json-viewer, 它是用于打印JSON和JSONP的Chrome 扩展 JSON查看器 你的眼睛所见过的最漂亮和可以定制的json/。 它是用于打印JSON和JSONP的Chrome 扩展。注释:这里扩展可能会与其他JSON萤火虫/格式化程序发生碰撞,你...

    vue-jsonp:一个用于处理JSONP请求的小型库

    $jsonp ( '/some-jsonp-url' , { myCustomUrlParam : 'veryNice'} ) 直接使用功能: import { jsonp } from 'vue-jsonp'jsonp ( '/some-jsonp-url' , { myCustomUrlParam : 'veryNice'} )发送数据并设置查询和函数...

    前端项目-fetch-jsonp.zip

    前端项目-fetch-jsonp,Fetch JSONP like a boss using Fetch API

    json-formatter, 使 json/jsonp易于阅读.zip

    json-formatter, 使 json/jsonp易于阅读 JSON格式化程序当你在浏览器选项卡中访问 in'直接'时,很好的打印JSON和tmodel的Chrome 扩展。特性JSONP支持快速,即使在长页面上有效的JSON页面- URL不重要语法高亮显示带有...

    jquery-jsonp

    jquery-jsonp插件

    axios-jsonp:适用于axios的jsonp适配器

    适用于axios的jsonp适配器支持承诺,支持取消,与xhr相同安装npm install axios-jsonp用法let axios = require('axios');let jsonpAdapter = require('axios-jsonp');axios({ url: '/jsonp', adapter: jsonpAdapter,...

    json-viewer:这是一个Chrome扩展程序,用于打印JSON和JSONP

    您所见过的最美丽,最可定制的JSON / JSONP荧光笔。 这是一个Chrome扩展程序,用于打印JSON和JSONP。 笔记: 此扩展名可能与其他JSON荧光笔/格式化程序一起崩溃,您可能需要禁用它们 要突出显示本地文件和隐身...

    jquery-jsonp-master.zip

    终于找到了一个好用的jquery-jsonp插件,可以完美的支持jsonp请求。可以完美的支持jsonp请求。

    Ajax-parse-jsonp.zip

    Ajax-parse-jsonp.zip,获取jsonp响应的有效负载。,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中网页的小部分在不重新加载网页的情况下...

    Ajax-cors-jsonp-sample.zip

    Ajax-cors-jsonp-sample.zip,客户端和服务器端(Java)用于跨源资源共享(CORS)的示例。,ajax代表异步javascript和xml。它是多种web技术的集合,包括html、css、json、xml和javascript。它用于创建动态网页,其中...

    第25周-第06章节-Python3.5-JSONP跨域请求jQuery方式.avi

    第25周-第06章节-Python3.5-JSONP跨域请求jQuery方式.avi

    JSON to JSONP- Bypass Same-Origin Policy

    JSON to JSONP- Bypass Same-Origin Policy

    12 React 获取数据 axios插件 fetch-jsonp插件的使用

    12 React 获取数据 axios插件 fetch-jsonp插件的使用(26分15秒)

    jquery-jsonp:jquery jsonp 插件

    网络故障或格式错误的JSON 响应时的错误恢复, 精确控制回调命名及其在 URL 中的传输方式, 多个具有相同回调名称的请求同时运行, 两种缓存机制(基于浏览器和基于页面), 可以像任何其他 AJAX 请求一样手动...

    跨域访问解决方法-jsonp

    使用jsonp跨域获取json数据。Ajax获取JAVA服务器json数据。

    axios-add-jsonp:向axios添加jsonp方法

    axios-add-jsonp 向axios添加jsonp方法安装$ npm install axios-add-jsonp --save例子统一所有请求import request from 'axios-add-jsonp'request . jsonp ( '/api/123' , { bar : foo , foo : bar } ) . then ( res...

    soundcloud-resolve-jsonp

    soundcloud-resolve-jsonp 使用带有 corslite 的 SoundCloud API 解析端点和用于 Safari 的 JSONP 回退 入门 npm install soundcloud-resolve-jsonp var resolve = require ( 'soundcloud-resolve-jsonp' ) ; var ...

    jquery-jsonp.js

    7、为了便于客户端使用数据,逐渐形成了一种非正式传输协议,人们把它称作JSONP,该协议的一个要点就是允许用户传递一个callback参数给服务端,然后服务端返回数据时会将这个callback参数作为函数名来包裹住JSON数据...

    sso-jsonp-demo

    jsonp实现sso跨域登录。这个代码实际上是自己写的一个demo。部署后,先在a项目login.jsp登录,然后再进入a和b项目的index.jsp就能看到已经登录。

    Ajax跨域请求解决方案-JSONP

    该资源为ASP.NET网站开发技术中使用Ajax进行跨域请求时利用JSONP方案解决浏览器同源策略限制的源码,比较简单,仅供参考

Global site tag (gtag.js) - Google Analytics