I am trying to communicate data between JavaScript and Python, and I am doing that by using JSON variables, but it seems that whenever I make a POST request from JavaScript, request.get_json() in the Python receiver method is not picking up anything and prints None when I print request.get_json().
The Python method works where it will return something back to JS, but it is always None. Am I doing something wrong in my $.post() method?
JavaScript $.post() call:
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 receiver method:
@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