【问题标题】:AJAX request not calling python-flask methodAJAX 请求不调用 python-flask 方法
【发布时间】: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() 用于GETPOSTsend(data) 用于POST。没有send(),您不会向服务器发送请求。

标签: javascript python ajax flask web-development-server


【解决方案1】:

您必须使用request.send() 向服务器发送请求。

使用GETPOSTPUTDELETEHEAD等都没关系


from flask import Flask, render_template_string, jsonify

app = Flask(__name__)

@app.route('/')
def index():
    return render_template_string('''
<button class="messaging-channel">SUBMIT</button>

<script>
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.`);
        };

        request.send();
    };
});
</script>''')

@app.route("/channel", methods=["POST", "GET"])
def channel():
    print("Working python\n\n")
    return jsonify({"channel": "Working"})

if __name__ == '__main__':
    app.run()

【讨论】:

  • python 函数现在被调用,但 request.onload 函数仍然有同样的问题。它仍然给我arguments:TypeError:'arguments','callee'和'caller'在这种情况下无法访问。 caller:TypeError:在此上下文中无法访问“arguments”、“callee”和“caller”。
  • 这段代码对我有用,没有问题。我没有尝试用调试器运行它,也许你所有的问题都只是调试器。或者问题可能更复杂,需要完整的代码才能看到这个错误。或者服务器可能有一些限制,运行代码会出现问题。可能你应该使用谷歌搜索onload TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context.也许有人已经遇到过这个问题。
  • 我在 Google 上找到了ReferenceError: deprecated caller or arguments usage,当 JavaScript 使用 'use strict'; 时它显示了这个问题
  • 我在 Google 上找到了AJAX Error: TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context,它显示X-origin restrictions 可以阻止它。
猜你喜欢
  • 1970-01-01
  • 2018-07-04
  • 2012-05-20
  • 1970-01-01
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多