0

I'm trying to understand how I can use Python and javascript so that I can use POST/GET commands. I have a button that sends a request to server-side python, and should return a value. I understand how to print out a response, but I want to pass a value to a javascript variable instead of just print thing the response.

For example, my javascript sends a string to the python file using the jquery POST function:

 <script>               
    function send(){

        $.ajax({ 
            type: "POST",
            url: "pythondb.py", 
            data:{username: 'william'},  
            async: false,
            success: function(){  
                alert('response received')              
            }, 
        dataType:'json'
        }); 
    }
  </script>

Then using the python cgi module I can print out the value of username:

#!/usr/bin/env python
import cgi
print "Content-Type: text/html"
print
form = cgi.FieldStorage()
print form.getvalue("username")

however I am not receiving the data in the same way that the php echo function works. Is there an equivalent to 'echo' for the python cgi module?

I have read this question which explains the different python frameworks that can be used to communicate between browser and server; for the moment I was hoping to keep things as simple as possible and to use the cgi module, but I don't know if that is the best option.

Community
  • 1
  • 1
djq
  • 14,810
  • 45
  • 122
  • 157

2 Answers2

1

Your success: function can take a parameter, to which jQuery will pass the contents of the response from the AJAX request. Try this and see what happens:

 <script>               
    function send(){

        $.ajax({ 
            type: "POST",
            url: "pythondb.py", 
            data:{username: 'william'},  
            async: false,
            success: function(body){  
                alert('response received: ' + body);              
            }, 
            dataType:'json'
        }); 
    }
  </script>

P.S. Why are you using async: false? That kind of defeats most of the point of AJAX.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Unfortunately that didn't work as no alert appeared. I'm using `async: false` as I want my program to wait for the values that are returned before proceeding. It works with my php script, but perhaps it is not a good style; however I'm not aware of an alternative way of doing it. – djq Oct 01 '11 at 14:50
  • Should I be using something like 'json' in my content type? – djq Oct 01 '11 at 14:52
  • shouldn't need to; jQuery will do its best to detect the right type. if you're not getting an alert at all, make sure your CGI is still responding? – Amber Oct 01 '11 at 14:58
  • I am getting a response from my CGI (firebug tells me that it returned 'william') but jQuery does not seem to pick it up. – djq Oct 01 '11 at 16:34
  • @celenius: failure to understand what asynchronous means. Of course your Javascript will wait for your values to be returned. But the rest of your page shouldn't have to, that's the whole point of Ajax. – Daniel Roseman Oct 01 '11 at 18:29
  • @DanielRoseman In the context I thought it made sense. The javascript queries a database for some values, and then generates bars on a chart. This seemed like an easy way to ensure that the function to plot these values is called at the correct time. My original problem remains though..... getting the JS to receive the output of `print` (or some other function). – djq Oct 02 '11 at 00:52
  • No. As Amber says, this defeats the point of using Ajax. Async means that the code is called when the query from the server returns, but *your page does not have to wait*. Otherwise, the whole page is unresponsive. Don't do it. – Daniel Roseman Oct 02 '11 at 08:31
0

Your json is not in proper json format.

According to http://api.jquery.com/jquery.parsejson/,

  • username needs to be in quotes
  • you can only use double quotes

It would look like this:

{"username": "william"}

Also, your alert should have a semi colon on the end. I can't guarantee this answer will fix your problem, but it may be that your data isn't getting passed to cgi at all.

NuclearPeon
  • 5,743
  • 4
  • 44
  • 52