简体   繁体   中英

How to extract array data from Ajax to Python flask?

I am having problem extracting data sent from the Ajax call to the python flask backend. I am able to extract the key:value string pair only from the request.args object. But the problem is the data I am sending is of mixed type : eg data:{'data':'stringdata','Email':['abc@example.com','def@example.com'],'checkbox':'[true,true]'}

when I am reading this data from the request.args in my python module.. I am able to get request.args['data'] which returns 'stringdata' but for the other keys like request.args['Email'] I am getting BAD REQUEST errors.

I have tried JSON.stringify the data separately for each item plus the whole data object but none of it is working. I have also tried setting the data type and the contentType in the Ajax call but still no good.

$.ajax
        ({
              url: '/abc',
              data : {
                  "data": 'stringdata',
 "Email": ['abc@example.com','def@example.com'] ,
 "Hbeat":[true,true],
 "Creport" : [true,true] ,
 "Treport" : [true,true] ,
 "Preport" : [true,true]
                },
              success: function(data) 
              {

              },
              complete: function() 
              {
              }
          });

Here the python part where I am trying to get the data.:

@app.route('/abc', methods=['POST','GET'])
def checkingdata():
    print(request.args)
    print(request.args['data'])
    print(request.args['Email']) # this is where I am getting errors.

I expect to get the data as the form they are in. eg where I am passing the list of emails I need to get them as list. and where there are single string values I should get them as single string values.

thanks so much for your time. Please help if you can.

您需要使用request.args.getlist

emails = request.args.getlist('Email')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM