0

I send the following data object to server side using jQuery ajax

Front-end

var updateData = { 'wrapper': [
        {"powerOff": {"typeA": "000"} }, 
        {"idle":     {"typeA": "010"} }, 
        {"running":  {"typeA": "001"} }, 
    ]}

$.ajax({
    url: '/giveItToMe',
    data: updateData,
    type: 'GET'
})

Flask back-end

@app.route('/giveItToMe')
def giveItToMe():
    res = request.args
    print(res)
    print('-----)
    for i in res:
        print(i)
        print(res[i])

this will result

ImmutableMultiDict([('wrapper[0][powerOff][typeA]', '000'), ('wrapper[1][idle][typeA]', '101'), ('wrapper[2][running][typeA]', '001')])
-----
wrapper[0][powerOff][typeA]
000
wrapper[1][idle][typeA]
010
wrapper[2][running][typeA]
001

But this is not what I want. The result is too dirty

Any good idea to extract request so that I can get each key-value pair separately like the following, or what ever more clean result

powerOff, typeA, 000
idle, typeA, 010
running, typeA, 001

Why I do this is because I will update data to mongodb, so I need to split each value from request object

MongoDB Before update

db.machine.find({}, {_id: 0})
{"powerOff": {"typeA": "000"} }
{"idle":     {"typeA": "111"} }
{"running":  {"typeA": "001"} }

MongoDB After update

db.machine.find({}, {_id: 0})
{"powerOff": {"typeA": "000"} }
{"idle":     {"typeA": "010"} }
{"running":  {"typeA": "001"} }
Tony Chou
  • 584
  • 1
  • 9
  • 26

0 Answers0