title: How to get AJAX posted JSON in flask?
body: <p>I am following this <a href="http://www.makeuseof.com/tag/python-javascript-communicate-json/" rel="nofollow noreferrer">flask tutorial</a> to learn about building an app in Python.</p>
  <p>The tutorial (close to the end) talks about how to get a AJAX posted json inside python as follows:</p>
  <p>HTML code:
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  // setup some JSON to use
  var cars = [
      { "make":"Porsche", "model":"911S" },
      { "make":"Mercedes-Benz", "model":"220SE" },
      { "make":"Jaguar","model": "Mark VII" }
  ];
  
  window.onload = function() {
      // setup the button click
      document.getElementById("theButton").onclick = function() {
          doWork()
      };
  }
  
  function doWork() {
      // ajax the JSON to the server
      $.post("receiver", cars, function(){
  
      });
      // stop link reloading the page
   event.preventDefault();
  }
  &lt;/script&gt;
  This will send data using AJAX to Python:&lt;br /&gt;&lt;br /&gt;
  &lt;a href="" id="theButton"&gt;Click Me&lt;/a&gt;</code></pre>
  </div>
  </div>
  </p>
  <p>Python Code:</p>
  <pre><code>import sys
  
  from flask import Flask, render_template, request, redirect, Response
  import random, json
  
  app = Flask(__name__)
  
  @app.route('/')
  def output():
      # serve index template
      return render_template('index.html')
  
  @app.route('/receiver', methods = ['POST'])
  def worker():
      # read json + reply
      data = request.get_json()
      result = ''
  
  
      for item in data:
          # loop over every row
          result += str(item['make']) + '\n'
  
      return result
  
  if __name__ == '__main__':
      # run!
      app.run()`
  </code></pre>
  <p>When I run the script and click on the button 'Click me' in the browser, I get the '500 Internal Server Error' when I examine the Response in the browser. If I print the data variable, it prints out None in the terminal on the click event. I tried the suggestions given in the comments to use get_json(forced=true) in python script and stringify the 'cars' json in html file but in vain.</p>