javascript - 将 JSON 从 jQuery .getJSON 传递到 Python

标签 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 相关的代码来获取数据,但我未能进行任何配置。有人有什么建议吗?

我对开发还很陌生,所以如果我错过了一些明显的东西,我深表歉意。

非常感谢

最佳答案

这对我有用:

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

希望它对你有用。

关于javascript - 将 JSON 从 jQuery .getJSON 传递到 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10232327/

相关文章:

python - python中的对象属性

javascript - 有没有更漂亮的方法来捕获 JSON 数组 block 上的错误?

javascript - jquery.kyco.googleplusfeed.min.js 未缩小的源代码

javascript - 在 Highcharts 中对堆叠条形图进行分组

jquery - 下拉菜单 - 将子项除以 2 并将它们放入两列

jquery - 在 jQuery 中提升多个父级 - 更有效的方法?

python - 更新动态 QGridLayout - Python PyQt

Python切片列表中的第一个和最后一个元素

javascript - 使用 jQuery 使用 Next Previous 按钮显示隐藏情侣 div

javascript - 在 CSS 和 HTML 中创建灯 zipper