Reputation: 63
I need some help with getting json data in flask from jquery ajax.
Below client side javascript which gets called whenever some button is clicked.
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
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)
});
}
</script>
on server side(flask), I have below code to catch json data.
@app.route('/robot/api/<item>', 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
"""
"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 -"
It shows request.content_type is json but when actually printed it says None.
I did packet capture to see data from PC with wireshark and I see below:
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"}
But Flask server returns "HTTP/1.0 400 BAD REQUEST"
Upvotes: 4
Views: 10380
Reputation: 159905
The issue here is that you are missing the "i" in "application/json". Change your jQuery call to contentType:"application/json"
(rather than contentType:"applicaton/json"
) and everything should work just fine.
Flask only loads the payload in the request body as JSON if the content type is application/json
- anything else and you have to load the JSON yourself, using request.get_json(force=True)
Upvotes: 6