title: ajax json post request to flask
body: <p>I am struggling to send a ajax post request but cannot figure out where i am going wrong, i have this form which i submit with js and along with that form i want to send the id of a div: </p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
        $(document).ready(function() {
          $('input[type=radio]').on('change', function() {
              $(this).closest("form").submit();
              var poll_id = $(this).closest("div").attr("id");
              var data = {poll_id};
              console.log(JSON.stringify(data));
              $.ajax({
                  url: '/poll',
                  type: 'POST',
                  data: JSON.stringify(data),
                  contentType: 'application/json',
                  dataType: 'json'
                }, function(data) {
                    console.log(data);
                  });
            });
      });
      &lt;/script&gt;
  </code></pre>
  
  <p>and in flask i try to request it with <code>request.get_json()</code> but keep getting error 400, the form and db.commit() works fine:</p>
  
  <pre><code>@app.route('/poll', methods=['GET', 'POST'])
  def poll():
      polltodb = pollingresult(request.form['points'])
      session['points_session'] = request.form['points']
      db.session.add(polltodb)
      db.session.commit()
      data = request.get_json()
      print data
  </code></pre>
  
  <p>but the get_json() fails.</p>