Can't write text sent by javascript to a .txt file using python cgi
Question: I'm having an error, while tring to write my string variable sent from
javascript to a .txt file in cgi. this is the python cgi code with the error:

    
    
              1  #!/usr/bin/python
              2
              3 import cgi, cgitb
              4 cgitb.enable()
              5
              6 print "Content-type: text/html"
              7 print
              8
              9 print "<html><head><title>Nimed</title></head><br/>"
             10  
             12 formdata = cgi.FieldStorage()
             13 tulemused = formdata.getvalue('tulemused')
             14 f = open('/home/t103692/public_html/prax3/tulemused.txt', 'a')
        =>   15 f.write(tulemused)
             16 f.close()
        f = <open file '/home/t103692/public_html/prax3/tulemused.txt', mode 'a'>, f.write =      <built-in method write of file object>, tulemused = None
    
        <type 'exceptions.TypeError'>: expected a character buffer object 
              args = ('expected a character buffer object',) 
              message = 'expected a character buffer object'
    

And here's the javascript code, where I send the variable:

    
    
        function saadaTulemused() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("POST","http://dijkstra.cs.ttu.ee/~t103692/cgi-bin/save.py", true);
        xmlhttp.setRequestHeader("content-type","application/x-www-form-urlencoded");
        xmlhttp.send(tulemused);
        }
    

"tulemused" is the string variable, which already contains some text without
spaces.

How can I solve the error?


Answer: `tulemused` is `None` and you cannot write that to a file.

You need to actually send `x-www-form-urlencoded` data from your JavaScript
handler; currently you only send the value and nothing else.

Prefixing the string with `'tulemused='` should be enough:

    
    
    xmlhttp.send("tulemused=" + tulemused);
    

