如何人们通常实施JSONP在Python? [英] How do people usually implement jsonp in python?

查看:192
本文介绍了如何人们通常实施JSONP在Python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想学习如何使用Python中使用JSONP。我用Google搜索周围任何有用的教程。然而,似乎没有那么多的资源在那里。

I would like to learn how to use jsonp with python. I googled around for any useful tutorial. However, it seems that there are no so much resources up there.

因此​​,我想如果有人知道的任何教程在这里提出,API,我可以使用,或任何的最佳做法。

Thus I would like to ask here if anybody knows any tutorial, API that I can use, or any best practices.

感谢您。

推荐答案

你的意思是支持的的有一个Python供电API或网站JSOP输出?

Do you mean supporting the generation of JSOP output with a Python-powered API or website?

这是pretty轻松支持。说你在 /some/resource.json API已经输出一些JSON EN codeD数据(例如,在code这是一个返回json.dumps(字典(A ='富')))。

That's pretty easy to support. Say your API at /some/resource.json already outputs some JSON encoded data (say, in the code it's a return json.dumps(dict(a='foo'))).

要支持JSONP你所要做的就是接受一个回调参数(比如 /some/resource.json&callback=some_func )。现在,如果你得到这个参数,而不是只返回JSON序列数据,它包装在一个函数调用:

To support JSONP all you have to do is accept a callback parameter (say /some/resource.json&callback=some_func). Now, if you get this parameter, instead of returning just the json serialized data, you wrap it in a function call:

  d = json.dumps(dict(a='foo'))
  return 'some_func(' + d + ');'

这样,调用Web客户端code可以在它的DOM只是自动插入脚本标签,从而神奇地加载JavaScript的功能。有意义吗?

That way, calling web-client code can simply auto insert script tags in its DOM to magically load your javascript 'function'. Make sense?

这篇关于如何人们通常实施JSONP在Python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆