【问题标题】:Flask route not receiving Ajax Call - SolvedFlask 路由未收到 Ajax 调用 - 已解决
【发布时间】:2021-08-14 04:08:40
【问题描述】:

我试图从我的 HTML 表单中获取 2 个输入并使用 Ajax 将它们发送到我的 @app.route("/sell)。 我对其他路线采取了相同的步骤,使用相同的方法,我从来没有遇到过这个问题。

这是结果,它会让你更好地理解 --> Image

一个输入是名称,另一个是数字。 知道我做错了什么吗?非常感谢任何帮助。

HTML 表单

<form>
    <div class="one form-group">
        <select id="stockInput" name="symbol">
        {% for stock in stocks %} 
            <option value="{{ stock.symbol }}"> {{ stock.name }}
        {% endfor %}
        <option value="" selected disabled>Select stock</option>
    </div>

    <div class="one form-group">
        <input autocomplete="off" id="sharesInput" class="form-control" name="shares" type="number" min="1" placeholder="Number of shares">
    </div>

    <button id="sell" type="submit" class="btn btn-primary">Sell</button>
</form>

AJAX 调用

<script>

$(document).ready(function(){

$('form').on('submit', function(event) {

        /* Ajax Call */
        $.ajax({
            data : {
                share : $('#stockInput').val(),
                quantity : $('#sharesInput').val()
            },
            type : 'POST',
            url : '/sell'
        })

        .done(function(data) { 
            if (data.error){

            }
            else {

            }
        });

        event.preventDefault(); 
    });
});

        </script>

烧瓶

@app.route("/sell", methods=["GET", "POST"])
@login_required
def sell():
        #Sell shares of stock
    username = User.query.get(int(session["user_id"]))
        # Get all stocks as objects portfolio
    stocks = Portfolio.query.filter_by(username=username.username).all()


    # Iterate over the stocks list to append the information needed in index.html table
    for stock in stocks:
        symbol = str(stock.symbol)
        shares = int(stock.shares)
        name = lookup(symbol)["name"]
        price = lookup(symbol)["price"]
        change = lookup(symbol)["change"]
        total = shares * price
        stocks.name = name
        stocks.price = usd(price)
        stocks.total = usd(total)

    if request.method == 'POST':
        
        # Lookup stock and save it's info
        share = lookup(request.form.get("symbol"))
        # Get # of shares
        quantity = int(request.form.get("shares"))

        if not share:
            return error("Stock not found", 403)
        elif quantity < 1:
            return error("Please enter the number of shares", 403)
            ... more lines of code


        
    return render_template("sell.html", stocks=stocks)

【问题讨论】:

    标签: jquery ajax flask flask-sqlalchemy ajaxform


    【解决方案1】:

    你在这里使用了错误的变量吗?试试这个

           # Lookup stock and save it's info
            share = lookup(request.form.get("share"))
            # Get # of shares
            quantity = int(request.form.get("quantity "))
    

    【讨论】:

    • 很好的观察,这绝对是问题之一。欣赏!
    • @Flo 我很高兴能帮助您找到部分解决方案。有时,当您盯着同一个问题看太久时,很容易忽略这样的愚蠢错误!欢呼
    【解决方案2】:

    问题解决了!

    我在 html 中的 Ajax 头文件不知何故消失了……

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    

    谢谢!

    【讨论】:

      猜你喜欢
      • 2019-07-10
      • 2018-12-11
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 2012-10-17
      • 2016-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多