title: ajax returns empty string instead of json [python cgi]
body: <p>Basically, I have a cgi script that prints out valid json, I have checked and I have a similar functions that work the same way but this one doesn't some reason and I can't find it.</p>
  
  <p>Javascript:</p>
  
  <pre><code>function updateChat(){ 
  $.ajax({
              type: "get",
              url: "cgi-bin/main.py",
              data: {'ajax':'1', 'chat':'1'},
              datatype:"html",
              async: false,
              success: function(response) {
                  alert(response); //Returns an empty string
              }, 
              error:function(xhr,err)
              {
                  alert("Error connecting to server, please contact system administator.");
              }
          });
  </code></pre>
  
  <p>Here is the JSON that python prints out:</p>
  
  <pre><code>[
  "jon: Hi.",
  "bob: Hello."
  ]
  </code></pre>
  
  <p>I used json.dumps to create the JSON it worked in previous functions that have pretty much the same JSON layout only different content.</p>
  
  <p>There is a whole bunch more of server code, I tried to copy out the relevant parts. Basically I'm just trying to filter an ugly chat log for learning purposes. I filter it with regex and then create a json out of it.</p>
  
  <pre><code>    #!/usr/bin/env python
      # -*- coding: UTF-8 -*-
  
      print "Content-type: text/html\n\n"
      print 
  
      import cgi, sys, cgitb, datetime, re, time, random, json
  
      cgitb.enable()
  
      formdata = cgi.FieldStorage()
  
  
      def tail( f, window=20 ): 
          BUFSIZ = 1024
          f.seek(0, 2)
          bytes = f.tell()
          size = window
          block = -1
          data = []
          while size &gt; 0 and bytes &gt; 0:
              if (bytes - BUFSIZ &gt; 0):
                  # Seek back one whole BUFSIZ
                  f.seek(block*BUFSIZ, 2)
                  # read BUFFER
                  data.append(f.read(BUFSIZ))
              else:
                  # file too small, start from begining
                  f.seek(0,0)
                  # only read what was not read
                  data.append(f.read(bytes))
              linesFound = data[-1].count('\n')
              size -= linesFound
              bytes -= BUFSIZ
              block -= 1
          return '\n'.join(''.join(data).splitlines()[-window:])
  
  
      def updateChatBox():
          try:
              f = open('test.txt', 'r')
              lines = tail(f, window = 20)
              chat_array = lines.split("\n")
              f.close()
          except:
              print "Failed to access data"
              sys.exit(4)
  
          i = 0
          while i &lt; len(chat_array):
              #remove timer
              time = re.search("(\[).*(\])", chat_array[i])
              result_time = time.group()
              chat_array[i] = chat_array[i].replace(result_time, "")
              #Removes braces around user
              user = re.search("(\\().*?(_)", chat_array[i])
              result_user = user.group()
              chat_array[i] = chat_array[i].replace("(", "")
              chat_array[i] = chat_array[i].replace(")", "")
              #Removes underscore and message end marker
              message = re.search("(_).*?(\|)", chat_array[i])
              result_message = message.group()
              chat_array[i] = chat_array[i].replace("_", ":")
              chat_array[i] = chat_array[i].replace("|", "")
              data += chat_array[i] + "\n"
              i = i + 1
          data_array = data.split("\n")
          json_string = json.dumps(data_array)
          print json_string
  
  
  
      if formdata.has_key("ajax"):
          ajax = formdata["ajax"].value
          if ajax == "1": #ajax happens
  
              if formdata.has_key("chat"):
                      chat = formdata["chat"].value
                      if chat == 1:
                          updateChatBox()
                  else:
                      print "ERROR"
                      elif formdata.has_key("get_all_stats"):
                              get_all_stats = formdata["get_all_stats"].value
                              if get_all_stats == "1":
                                  getTopScores()
                      else:
                          print "ERROR"
  </code></pre>
  
  <p>Here is also a function that works perfectly and is in the same python file</p>
  
  <pre><code>def getTopScores():
      try:
          f = open('test_stats.txt', 'r')
          stats = f.read()
          stats_list = stats.split("\n")
          f.close()
      except:
         print "Failed reading file"
         sys.exit(4)
  
      json_string = json.dumps(stats_list)
      print json_string
  </code></pre>
  
  <p>The only difference is using the tail function and regex, the end result JSON actually looks identical.</p>