0
我的AJAX调用看起来是这样的:如何在Python中接收对象数组作为参数使用AJAX作为HTTP请求传递Flask?
$.ajax({
url: "/doSomeCoolThingOnServer",
type: "POST",
async: false,
data: {
simple_string: "Variable sent from client side",
array_of_strings: ["John", "George"],
array_of_objects: [
{ city: "Shanghai", population: 1000 },
{ city: "Budapest", population: 2501 }
]
},
success: function(response) {
console.log("===== SUCCESS =====");
console.log(response);
},
error: function(response) {
console.log("===== ERROR =====");
console.log(response);
}
});
我想收到数组对象如Python的类型的字典的数组的,但我有空数组返回。
@app.route("/doSomeCoolThingOnServer", methods=['POST'])
def doSomeCoolThingOnServer():
simple_string = request.form['simple_string']
array_of_strings = request.form.getlist('array_of_strings[]')
array_of_objects = request.form.getlist('array_of_objects[]')
print(simple_string) #returns desired string
print(array_of_strings) # returns desired array
print(array_of_objects) # returns empty array
请指教如何在Python中接收对象数组作为参数Flask作为HTTP POST请求使用AJAX传递?