将JSON从jQuery .getJSON传递给Python [英] Passing JSON from jQuery .getJSON to Python

查看:86
本文介绍了将JSON从jQuery .getJSON传递给Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been prepping for a potential move away from PHP and decided to use Python for my next project, I have managed to got myself completely stuck on something and I wonder if anybody could help me out.

如果我执行:

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

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

In 'test.php' I can use

$_GET['testdata']

检索'hello world'。

to retrieve 'hello world'.

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

I cannot seem to find the equivalent for test.py:

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

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

I have seen code posted online pertaining to request.get or os.environment to get the data but i have failed to make any configuration work. Would anybody have any suggestions?

我对开发很新,所以如果我错过了一些明显的东西,我会道歉。

I'm pretty new to development, so apologies if i'm missing out something obvious.

亲切的谢谢

推荐答案

这对我有用:

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

I希望它适合你。

这篇关于将JSON从jQuery .getJSON传递给Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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