Click to See Complete Forum and Search --> : Ajax Python cgi and parameters


keirvt
09-10-2010, 07:28 PM
I am trying to write an Ajax script to send parameters to a server side Python cgi script.
I am required to use Python for cgi although some tests I did show that php just works

i need to send parameter to the script and get stuff back to Ajax at the client

At first tried code to only obtain a string back from the python cgi, the ajax call code is

ajaxRequest.open("GET", "text.py", true);

The Python code was at first
#!/usr/bin/python
print "A string"

The first problem with the Python cgi is that the web server gave a file not found error apparently unable to locate the text.py file even though it was definitely there with correct permissions and ownership. After googling about I found a rare comment that the Python script has to contain a function called def index()
This is correct, the code, text.py, that is then recognised is

def index(req):
wp="whatever"
return wp

And I get my desired text returned to the Ajax call.
Now I want to pass parameters to the python script.
The ajax code to call the cgi scipt is

var queryString="?param1=keyName"
ajaxRequest.open("GET", "text.py"+queryString, true);

The python code is now

#!/usr/bin/python
import cgi
def index(req):
comms=cgi.FieldStorage()
if comms.has_key("param1"):
wp="Found the key"
else:
wp=comms.keys()
return wp

This debug code should show a success at finding a particular key name or return whatever key there may happen to be.

Each time the key set is returned, it is an empty list. The python program is not receiving any of the paremeters

I've tried all kinds of variations importing environs etc etc
Has anyone got a piece of Python code that works in both directions using Ajax?