I'm trying to call an AJAX request with XMLHttpRequest() from JavaScript to a flask server but the problem is that request.open()
doesn't seem to work as the python function is not being called.
The .js code is
document.addEventListener("DOMContentLoaded", () => {
document.querySelector(".messaging-channel").onclick = () => {
const channelName = document.querySelector(".messaging-channel").innerHTML;
var request = new XMLHttpRequest();
request.open("POST", "/channel");
window.alert(`${channelName}`);
request.onload = () => {
const response = JSON.parse(request.responseText);
const messages = response.channel;
window.alert(`${messages} are the messages.`);
};
};
});
and the flask method is
@app.route("/channel", methods=["POST", "GET"])
def channel():
print("Working python\n\n")
return jsonify({"channel": "Working"})
Both of the files are connected and window.alert(`${channelName}`);
executes fine in the .js file. Though the request is not calling the python function. I also checked this on the debugger by safari and it would say that:
arguments: TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context.
caller: TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context.