Dom Vinyard
Dom Vinyard

Reputation: 2088

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

Upvotes: 0

Views: 1841

Answers (2)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

The cgi documentation contains information on how to retrieve GET and POST variables in a Python CGI script.

Upvotes: 0

Gustavo Vargas
Gustavo Vargas

Reputation: 2597

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.

Upvotes: 2

Related Questions