jquery - how to get data from 'ImmutableMultiDict' in flask -
i learning how use ajax , flask ,so send ajax request , receive data post request in python file
my html file contains code
var data = {"name":"john doe","age":"21"}; $.ajax({ url:'/post/data', datatype : "json", contenttype: "application/json; charset=utf-8", data : json.stringify(data), success : function(result) { jquery("#clash").html(result); },error : function(result){ console.log(result); } }); and python file contains :
@app.route('/post/data',methods=['get','post']) def postdata(): #do data = str(request.args) json_dumps = json.dumps(data) return json_dumps this gives me following data on page
"immutablemultidict([('{\"name\":\"john doe\",\"age\":\"21\"}', u'')])" and request.query_string looks {%22name%22:%22john%20doe%22,%22age%22:%2221%22}
so how name , age. please correct me if wrong anywhere.thanks in advance.
you don't need data immutablemultidict. there couple of errors in have preventing pulling response json data. first off, have tweak parameters of ajax call. should add in call type post. furthermore, datatype should spelt datatype. new call should be:
var data = {"name":"john doe","age":"21"}; $.ajax({ type: 'post', contenttype: 'application/json', url: '/post/data', datatype : 'json', data : json.stringify(data), success : function(result) { jquery("#clash").html(result); },error : function(result){ console.log(result); } }); the data being sent post request json type. on flask server, can read data son information follows:
@app.route('/post/data',methods=['get','post']) def postdata(): jsondata = request.get_json() print jsondata['name'] print jsondata['age'] return "hello world" #or whatever want return this print john doe , 21 successfully.
let me know if works or if have additional questions!
edit: can return success ajax call flask follows:
# include import @ tomb flask import jsonify @app.route('/post/data',methods=['get','post']) def postdata(): ... return jsonify(success=true, data=jsondata)
Comments
Post a Comment