开发者

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

开发者 https://www.devze.com 2023-02-21 05:17 出处:网络
Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this
Closed. This question needs to be more focused. It is not currently accepting answers.

Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 9 years ago.

开发者_Go百科 Improve this question

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.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号