简体   繁体   中英

Passing JSON from jQuery .getJSON to 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.

If I perform:

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

In 'test.php' I can use

$_GET['testdata']

to retrieve 'hello world'.

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

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

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.

Kind thanks

This works to me:

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 hope it works for you.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM