The web foreground sends a POST request by JavaScript through Ajax, and when the request data is an array, the Python Flask server is parsed as follows:
Js:
1 varids = [];2 for(vari = 0; i < row.length; i++) {3 Ids.push (row[i].id);4 }5Console.log (' del: ' +IDs);6 $.ajax ({7Method: ' POST ',8URL: '/dance_del_data ',9DataType: ' JSON ',Ten data: {' IDs ': IDs, ' Who ': Datagridid}, OneSuccessfunction(data) { A$ (DG). DataGrid (' Reload '); -Console.log (' success in Ajax. ')) - }, theErrorfunction () { -Console.log (' Error in Ajax. ')); - }
-});
Flask:
1@app. Route ('/dance_del_data', methods=['POST'])2 defdance_del_data ():3 #if Request.has_key (' IDs '):4who =request.form['who'] 5ids =request.form.getlist ('ids[]') 6 forIinchIDs:7 Db.session.delete (DanceStudent.query.get (i))8 Db.session.commit ()9 Ten return "OK for del"
Summarize:
Non-array data, directly using request.form['key ') to obtain data. However, when the current station is sending an array, it is necessary to use the GetList function and the key plus [] to parse, and: Request.form.getlist ('key[]')
As for why to increase [], I have not found the reason, have known friends, please inform.
Flask parsing Web-side request arrays