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

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

问题描述

我想学习如何在 python 中使用 jsonp.我搜索了任何有用的教程.不过,上面好像没有那么多资源.

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?

这很容易支持.假设你在 /some/resource.json 的 API 已经输出了一些 JSON 编码的数据(比如,在代码中它是一个 return json.dumps(dict(a='foo'))代码>).

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 客户端代码可以简单地在其 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?

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

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