title: Decoding JSON with python using Appengine
body: <p>I have the following code which retrieves values from a simple 3 input form:</p>
  
  <pre><code>//retrieves data from a form
  var $form = $( this ),
      prgname= $form.find('input[name="prg"]').val(),
      startDate = $("#startdate").datepicker({ dateFormat: 'yy-mm-dd' }).val(),
      endDate = $("#enddate").datepicker({ dateFormat: 'yy-mm-dd' }).val();
  </code></pre>
  
  <p>The following code sends the request to the server:</p>
  
  <pre><code>var request = $.ajax({
        url: "/prg/",
        type: "post",
    data: JSON.stringify({prg:  prgname, start:startDate, end:endDate}),
    contentType: 'application/json',
    dataType: 'json',
        success: function() {},
        error: function (jqXHR, textStatus, errorThrown){};
  </code></pre>
  
  <p>on the server end using python and webapp2 Im doing the following, (here is where I am unsure of things)</p>
  
  <pre><code>import json
  
  class PrgHandler(webapp2.RequestHandler):
      def post(self):
          prg= cgi.escape(self.request.POST['prg'])
          start_date = cgi.escape(self.request.POST['start'])
          end_date = cgi.escape(self.request.POST['end'])
  
          #some code to write to db here
          ....
          #if successful return a success message
          if success:
              success_msg = [{'class': 'success', 'msg': 'Successfully saved to the database'}]
          else:
              success_msg = [{'class': 'error', 'msg': 'Could not saved to the database'}]
  
          data_string = json.dumps(success_msg)
          self.response.headers.add_header('content-type', 'application/json', charset='utf-8')   
          self.response.write(data_string)
  </code></pre>
  
  <p>When I get the response it is skipping the success function and going directly to the error.</p>
  
  <p>Logging the error values im not getting any thing meaningful:</p>
  
  <pre><code>the error is:
  The text status is:error
  The jqXHR is:[object Object] 
  </code></pre>
  
  <p>Chrome's console is giving me the error: </p>
  
  <pre><code>Resource interpreted as Document but transferred with MIME type application/json:
  </code></pre>
  
  <p>I looked that up and the solutions on SO did not work, I think this is an error with the server side code:</p>
  
  <pre><code>self.response.headers.add_header('content-type', 'application/json', charset='utf-8')
  </code></pre>
  
  <p>If I comment out the above line I get no error in chrome and I just get back the response on a blank page with the correct values in the following format: </p>
  
  <pre><code>[{"msg": "Successfully saved to the database", "class": "success"}]
  </code></pre>
  
  <p>In the above case it does save to the database so I cannot seem to find anything wrong except for the header and simply don't know how to proceed!</p>
  
  <p><strong>EDIT</strong>
  The error it seems is from the server side I had removed the following line:
      event.preventDefault();</p>
  
  <p>from my script and it caused all the problems now at least Im getting a clear indication of where the problem is. It's from incorrectly getting the posted data, how would I do it the correct way? I tried the following:</p>
  
  <pre><code>json_data = self.request.GET.items()
  decoded = json.loads(json_data)
  </code></pre>
  
  <p>but Im getting a TypeError: expected string or buffer on the following line:
      json_data = self.request.GET.items()</p>