Flask - 表单提交错误请求错误

时间:2017-07-02 18:52:33

标签: jquery python

我正在尝试使用Flask提交HTML表单。但是,它给了我错误的请求错误(400)。我无法弄清楚问题是什么。 这是我的HTML代码:

<div class="modal-body">

                <!-- content goes here -->
                <form class="add-user-form" name="add-user-form">
                    <div class="form-group" >
                        <label for="username">Email address</label>
                        <input type="email" class="form-control" name="username" id="username" placeholder="Enter email">
                    </div>
                    <div class="form-group">
                        <label for="password">Password</label>
                        <input type="password" class="form-control" name="password" id="password" placeholder="Password">
                    </div>
                    <div class="form-group">
                        <label for="brandName">Brand Name</label>
                        <input type="text" class="form-control" name="brandName" id="brandName" placeholder="Brand Name">
                    </div>
                    <div class="form-group">
                        <label for="status">Status</label>
                        <input type="text" class="form-control" name="status" id="status" placeholder="Status">
                    </div>
                    <div class="form-group">
                        <label for="msgDashboard">Message on Dashboard</label>
                        <input type="text" class="form-control" name="msgDashboard" id="msgDashboard" placeholder="Message on Dashboard">
                    </div>
                    <div class="form-group">
                        <label for="reportAccess">Report Access</label>
                        <input type="text" class="form-control" name="reportAccess" id="reportAccess" placeholder="Report Access URL">
                    </div>
                    <button type="submit" class="btn btn-default add-user" name="add-user">Submit</button>
                </form>
            </div>

这里有JQuery部分:

$('.add-user').click(function(event) {
        event.preventDefault();
        $.ajax({
            url: '/add-user',
            data: $('.add-user-form').serialize(),
            type: 'POST',
            success: function(response) {
                console.log(response);
                window.location.href = 'dashboard';
            },
            error: function(error) {
                console.log(error);
            }
        });
    });

这是我的Flask Code:

@app.route('/add-user', methods=['POST'])
def add_dashboard():
    if 'username' in session:
        user_info = DashboardUser(session['username'], db)
        user_info.add_user(request.form['username'], request.form['password'],
                           request.form['brandName'], request.form['parentCompany'],
                           request.form['status'])
        user_info.add_dashboard(request.form['username'], request.form['reportAccess'], request.form['msgDashboard'])
        return "Success"

有人可以帮助我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

好的,我认为您的问题是您只是返回一个字符串“Success”而不是返回某种真实的Web响应。

您应该使用Flask make_response方法:

from flask import make_response

@app.route('/add-user', methods=['POST'])
def add_dashboard():
    if 'username' in session:
        user_info = DashboardUser(session['username'], db)
        user_info.add_user(request.form['username'], request.form['password'],
                           request.form['brandName'], request.form['parentCompany'],
                           request.form['status'])
        user_info.add_dashboard(request.form['username'], request.form['reportAccess'], request.form['msgDashboard'])
        return make_response("Success")