title: getting json in flask from jquery ajax
body: <p>I need some help with getting json data in flask from jquery ajax.</p>
  
  <p>Below client side javascript which gets called whenever some button is clicked.</p>
  
  <pre><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
      function change_status(item, status) {
            var statusObj={"desc":item, "status":status};
            $.ajax({
                      url:"/robot/api/"+item,
                      type: "PUT",
                      contentType:"applicaton/json",
                      dataType:"json",
                      data: JSON.stringify(statusObj)
            });
      }
  &lt;/script&gt;
  </code></pre>
  
  <p>on server side(flask), I have below code to catch json data.</p>
  
  <pre><code>@app.route('/robot/api/&lt;item&gt;', methods = ['PUT'])
  def update_item(item):
  
  print "content_type: ", request.content_type
  
  print "request.json: ", request.json
  
  if not request.json:
      print "bad json format"
      abort(400)
  else
      """
      do something like update data
      """
  </code></pre>
  
  <h2>In PC, when clicking button in the browser which calls javascript function, flask server is printing below message:</h2>
  
  <pre><code>"content_type:  applicaton/json; charset=UTF-8
  
  request.json:  None
  
  bad json format
  
  192.168.1.241 - - [12/Feb/2014 15:06:16] "PUT /robot/api/3g HTTP/1.1" 400 -"
  </code></pre>
  
  <p>It shows request.content_type is json but when actually printed it says None.</p>
  
  <p>I did packet capture to see data from PC with wireshark and I see below:</p>
  
  <h2>From PC to Flask Server:</h2>
  
  <pre><code>PUT /robot/api/3g HTTP/1.1
  
  Host: "my flask server ip address"
  
  User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:26.0) Gecko/20100101 Firefox/26.0
  
  Accept: application/json, text/javascript, */*; q=0.01
  
  Accept-Language: en-US,en;q=0.5
  
  Accept-Encoding: gzip, deflate
  
  Content-Type: applicaton/json; charset=UTF-8
  
  X-Requested-With: XMLHttpRequest
  
  Referer: "my flask server ip address"
  
  Content-Length: 28
  
  Connection: keep-alive
  
  {"desc":"3g","status":"off"}
  </code></pre>
  
  <p>But Flask server returns "HTTP/1.0 400 BAD REQUEST"</p>