如何人们通常实施JSONP在Python? [英] How do people usually implement jsonp in 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屋!