【发布时间】:2020-09-29 11:10:28
【问题描述】:
我正在尝试使用 XMLHttpRequest() 从 JavaScript 向烧瓶服务器调用 AJAX 请求,但问题是 request.open() 似乎不起作用,因为没有调用 python 函数。
.js 代码是
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.`);
};
};
});
烧瓶方法是
@app.route("/channel", methods=["POST", "GET"])
def channel():
print("Working python\n\n")
return jsonify({"channel": "Working"})
两个文件都已连接,window.alert(`${channelName}`); 在 .js 文件中执行良好。虽然请求没有调用 python 函数。我还通过 safari 在调试器上检查了这一点,它会说:
arguments: TypeError: 'arguments', 'callee', and 'caller' cannot be access in this context.
caller: TypeError: 'arguments', 'callee', 和 'caller' 不能在这个上下文中被访问。
【问题讨论】:
-
也许你应该使用
request.send() -
不是 GET 而是我发送 POST 方法
-
send()用于GET和POST,send(data)用于POST。没有send(),您不会向服务器发送请求。
标签: javascript python ajax flask web-development-server