I am attempting to pass structured data from a web page to a flask handler, but I am getting key errors.
In the web page's JavaScript, I have an object that has two elements, one a value, the other an object.
The problem is on the Flask/Python side. I can get the first element ( "cid"
) with this:
print request.form ["cid"]
That works. But, I can't access or get to any of the att(ribute) values ( "att"
).
So, this, request.form ["att"]
returns KeyError: 'att'
I am able to access request.form["att[sw1]"]
but this is flattening my data type.
I'd like to get the att elements as a list or dictionary so that I can loop through them in my application. I essentially want request.form["att"]["sw1"]
or request.form.att["sw1"]
.
var controllerData = {
sw1 : $('#sw1').val(),
sw2 : $('#sw2').val(),
sw3 : $('#sw3').val(),
sw4 : $('#sw4').val()}
var updateData = {cid : 1, att: controllerData };
$.ajax({
type: "POST",
url: "/SetSettings",
data: updateData,
success: function(d) { },
error: function(d) {alert('Error saving settings!');},
dataType: "json"
});
You must serialize your data object to string with JSON.stringify() .
$.ajax({
type: "POST",
url: "/SetSettings",
data: JSON.stringify(updateData),
success: function(d) { },
error: function(d) {alert('Error saving settings!');},
dataType: "json"
});
PS: Some object, eg window
or object containing cyclic dependencies cannot be serialized this way
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.