How do people usually implement jsonp in python? [closed]

断了今生、忘了曾经 提交于 2019-12-20 12:35:18

问题


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.

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

Thank you.


回答1:


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

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'))).

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 + ');'

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



来源:https://stackoverflow.com/questions/5481926/how-do-people-usually-implement-jsonp-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!