Method Not Allowed.The 方法不允许用于请求的 URL。在烧瓶上

Method Not Allowed.The method is not allowed for the requested URL. On Flask

在 cmd 中使用了以下内容:

    set FLASK_APP=hello_app.py
    flask run --host=0.0.0.0

代码: '''

<input id="name-input" type="text"/>
<button id="name-button">Submit</button>
<p id="greeting"></p>

<script href="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<script>
    $('#name-button').click(function(event){
        let message = {
            name: $('#name-input').val()
        }
        $.post('http://192.168.1.106:5000/hello', JSON.stringify(message), function(response){
            $('#greeting').text(response.greeting);
            console.log(response);
        });
    });
</script>

'''

    from flask import Flask
    from flask import jsonify
    from flask import request

    app = Flask(__name__)
    @app.route('/hello', methods=['POST'])
    def hello():
        message = request.get_json(force=True)
        name = message['name']
         response = {
            'greeting':'Hello,' + name + '!'
        }
        return jsonify(response)

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

运行 时出现 405 错误。已经尝试将 'GET' 添加到导致错误请求的方法中。 有人可以帮忙吗?

将方法更改为 ['GET','POST']

@app.route("/home",methods=['GET','POST'])

然后

处理 post 数据
if request.method=='POST':
    #handle post data here
else:
    #return a normal page
    return "home"

也把最后一个 if 'name' == 'main': 改成这个 if __name__ == '__main__':

编辑: 在句柄 post 数据部分

添加您的 jsonify 代码