Reputation: 145
I am trying to do a JSON post to my Bottle server.
from bottle import request, route, run
@route('/feedback', method='POST')
def feedback():
data = request.json
print data
run(host='localhost',port=8080)
In the client side, I have
$('#user_feedback').submit(function() {
var feedback = {"id": 1}
$.ajax({
type: "POST",
url: "http://localhost:8080/feedback",
data: feedback
});
return false;
});
I am returning false here because I don't want the page to be redirected.
However, the data
I received in my Bottle server is always None
when printed out.
Please help. Thank you.
Upvotes: 2
Views: 4463
Reputation: 12785
request.json
expects the content type of the request to be application/json
.
So to make it work, you should set the contentType
property of your request to application/json
and stringify your data :
$.ajax({
type:"POST",
contentType: "application/json",
url:"/feedback",
data: JSON.stringify(feedback)
});
Upvotes: 3