I try to send json data from JavaScript to the flaskapp. But the json data sent from JavaScript is not accepted by the flask, the request is null, where is the wrong?
Here is my flask code.
@main.route('/getjson', methods = ['GET', 'POST'])
def getjson():
a = request.json
return jsonify(user = a)
Here is my javascript code.
$(function(){
$("#test").click(function(){
$.ajax({
url: "{{ url_for('main.getjson') }}",
type: "POST",
data: JSON.stringify({
"n1": "test1",
"n2": "test2",
"n3": "test3"
}),
dataType: "json",
success: function(data){
var a = data.user
var texthtml = "<p>" + a + "</p>"
$("#result").html(texthtml)
}
});
});
});
The data returned on the page is always null. Request.arg.get also does not work.