将JSON从jQuery .getJSON传递给Python

时间:2012-04-19 16:15:19

标签: javascript jquery python json

我一直在准备远离PHP,并决定将Python用于我的下一个项目,我已经设法让自己完全陷入困境,我想知道是否有人可以帮助我。

如果我执行:

$.getJSON('test.php', { testdata: 'hello world' })

在'test.php'中我可以使用

$_GET['testdata']

检索'hello world'。

我似乎无法找到test.py的等价物:

$.getJSON('test.py', { testdata: 'hello world' })

我已经看到在线发布的与request.get或os.environment相关的代码来获取数据,但我没有使任何配置工作。有人会有什么建议吗?

我对发展很陌生,如果我错过了一些明显的东西,那就道歉了。

谢天谢地

2 个答案:

答案 0 :(得分:2)

这对我有用:

import cgi
import cgitb     #this...
cgitb.enable()   #..and this are not really necessary but helps debuging

data= cgi.FieldStorage()

#remove this commant to take a look in the data received by python
#print data

#here you can retrieve the value passed by ajax
print data['testdata'].value

我希望它适合你。

答案 1 :(得分:0)

The cgi documentation包含有关如何在Python CGI脚本中检索GET和POST变量的信息。