title: Simple Python and Ajax Example How to Send Response with Python?
body: <p>I am testing out some code with Python and Javascript trying to get an Ajax system set up.  Basically I just want to input a word and have the python code send it back.  Here is my html/javascript:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Simple Ajax Example&lt;/title&gt;
  &lt;script language="Javascript"&gt;
  function xmlhttpPost(strURL) {
      var xmlHttpReq = false;
      var self = this;
      // Mozilla/Safari/Chrome
      if (window.XMLHttpRequest) {
          self.xmlHttpReq = new XMLHttpRequest();
      }
      // IE
      else if (window.ActiveXObject) {
          self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
      }
      self.xmlHttpReq.open('POST', strURL, true);
      self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      self.xmlHttpReq.onreadystatechange = function() {
          if (self.xmlHttpReq.readyState == 4) {
              updatepage(self.xmlHttpReq.responseText);
          }
      }
      self.xmlHttpReq.send(getquerystring());
  }
  
  function getquerystring() {
      var form     = document.forms['f1'];
      var word = form.word.value;
      qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
      return qstr;
  }
  
  function updatepage(str){
      document.getElementById("result").innerHTML = str;
  }
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;form name="f1"&gt;
    &lt;p&gt;word: &lt;input name="word" type="text"&gt;  
    &lt;input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/ajaxtest")'&gt;&lt;/p&gt;
    &lt;div id="result"&gt;&lt;/div&gt;
  &lt;/form&gt;
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>and here is my python:</p>
  
  <pre><code>class AjaxTest(BlogHandler):
      def get(self):
          user = self.get_user()       
          self.render('ajaxtest.html', user = user)
      def post(self):
          user = self.get_user()
          word = self.request.get('w')
          logging.info(word)
          return '&lt;p&gt;The secret word is' + word + '&lt;p&gt;'
          #having print instead of return didn't do anything
  </code></pre>
  
  <p>When I do logging the word shows up correctly and when I hardcode str in:</p>
  
  <pre><code>function updatepage(str){
      document.getElementById("result").innerHTML = str;
  }
  </code></pre>
  
  <p>It displays that correctly but right now without hardcoding it shows nothing.  How am I supposed to send the response?  I am using webapp2 as my Python framework and Jinja2 as the templating engine, though I don't think that has much to do with it.  Do I need to send the HTTP headers?</p>