How to get data from "ImmutableMultiDict" in a bulb

I am learning how to use ajax and Flask, so I send an ajax request and get the data as a post request in my python file

My html file contains this 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 my python file contains:

 @app.route('/post/data',methods=['GET','POST']) def postdata(): #do some data = str(request.args) json_dumps = json.dumps(data) return json_dumps 

This gives me the following data on the page

 "ImmutableMultiDict([('{\"name\":\"John Doe\",\"age\":\"21\"}', u'')])" 

And here is what my request.query_string looks like {%22name%22:%22John%20Doe%22,%22age%22:%2221%22}

So how do I get name and age . Please correct me if I am wrong. Thanks in advance.

+6
source share
3 answers

You really don't need to get data from ImmutableMultiDict . There are a few errors in what you have that prevent you from simply pulling the response as json data. First, you need to slightly adjust the parameters of your ajax call. You must add to the call type as POST . In addition, datatype should be written as datatype . Your new challenge 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); } }); 

Now the data is actually sent as a mail request with the json type. On the Flask server, we can now read data as information about the son as 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 you want to return 

This will lead to the successful completion of the printing of John Doe and 21 .

Let me know if this works for you or if you have any further questions!

Edit: you can return success to the ajax call from the bulb as follows:

 # include this import at the tomb from flask import jsonify @app.route('/post/data',methods=['GET','POST']) def postdata(): ... return jsonify(success=True, data=jsonData) 
+12
source

I came to this page because I am trying to submit a form with AJAX, and I finally found a solution. And the solution is to skip JSON (hope this helps others in the same search):

 $.ajax({ type: "POST", url: my_url, data: $("#formID").serialize(), //form containing name and age success: function(result){ console.log(result); } }); 

Then on the Flask server:

 app.route('/my_url', methods = [POST]) def some_function(): name = request.form['name'] age = request.form['age'] # do what you want with these variables return 'You got it right' 
+4
source

Just call to_dict for the request.form object, e.g. http://www.seanbehan.com/how-to-get-a-dict-from-flask-request-form/

0
source

Source: https://habr.com/ru/post/983892/


All Articles