In python CGI, how to pass "hello" back to a javascript function asan argument at client side?

Z

zxo102

Hi everyone,
How can I pass a string generated from python cgi at server side
to a
javascript function as an argument at client side?

Here is my case:

1. client side:
"load" is a javascript function in a html page. It starts the
python CGI "test.py" via Apache:
<html>
<script language="javascript">
....
load("test.py" );
....
</script>
....
</html>

I want test.py to "return" a "hello" back so the javascript function
load takes "hello" as argument like load("hello").

2. server side: test.py
....
#!c:\python24\python.exe

def main():
message = 'hello'
#return message

main()
....

Any ideas?


Thanks in advance for your help.


ouyang
 
B

Bruno Desthuilliers

zxo102 a écrit :
Hi everyone,
How can I pass a string generated from python cgi at server side
to a
javascript function as an argument at client side?

This is common HTTP / javascriot stuff - nothing related to Python.
First learn about the HTTP protocol - something you obviously need if
doing web development -, then eventually google for XMLHttpRequest (or
'ajax').

To make a long story short: the client side doesn't give a damn about
how the server-side works - it requests an url (-> read the part about
"HTTP Request" in the rfc), waits for the response (-> read the part
about "HTTP Response" in the rfc), and whatever it wants with the
response. FWIW, HTTP requests can have a "query string" containing
arguments.

From the server-side POV, your CGI script doesn't "return" anything -
it generates a HTTP response, which will be sent back to the client by
Apache.
 
P

Piet van Oostrum

zxo102 said:
z> Hi everyone,
z> How can I pass a string generated from python cgi at server side
z> to a
z> javascript function as an argument at client side?
z> I want test.py to "return" a "hello" back so the javascript function
z> load takes "hello" as argument like load("hello").
z> 2. server side: test.py
z> ...
z> #!c:\python24\python.exe
z> def main():
z> message = 'hello'
z> #return message
z> main()
z> ...
z> Any ideas?

CGI scripts return stuff by printing it, or more generally writing to
stdout.

So print message should do. The rest is not a Python question but a
Javascript question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top