我正在尝试在JavaScript和Python之间通信数据,并且正在使用JSON变量进行通信,但是似乎每当我从JavaScript发出POST
请求时,Python接收器中的request.get_json()
方法是不拾取任何东西,并且在打印None
时打印request.get_json()
。
Python方法可以在将返回的内容返回给JS的情况下使用,但始终为None
。我在$.post()
方法中做错什么了吗?
JavaScript $.post()
调用:
var items = {"robotCoor": {"robot_x": 1, "robot_y": 1},
"gridParams": {"goal_y": 0, "size_x": 16, "size_y": 16, "goal_x": 0},
"obsParams": {"obs_x3": 0, "obs_x4": 0, "obs_y4": 0, "obs_x2": 0, "obs_x1": 0, "obs_y1": 0, "obs_y2": 0, "obs_y3": 0},
"aiParams": {"layers": 0, "learning_rate": 0, "speed": 0}
};
var stuff = JSON.stringify(items);
console.log(stuff); #prints the correct thing
$.post("receiver", stuff, function( data ) {
console.log(stuff); #prints the correct thing
alert("Data: " + data); #alerts with "Data: None"
});
Python接收器方法:
@app.route("/receiver", methods = ["POST"])
def receiver():
if request.method == "POST":
stuff = request.get_json()
print(stuff) #prints None
stuff = str(stuff)
print(stuff) #prints None
return stuff