首页 > 解决方案 > 当使用 AJAX 从烧瓶返回值时,它显示 [Object, Object]

问题描述

所以我试图让我的项目通过 API 实时更新价格。我设法做到了,但价格本身是浮点值(int 和字符串工作得很好!)。我跑过去看看可能是什么问题,上面说console.log和我回来了。print()print()<class 'dict'>console.logObject

我猜我必须改变一些东西才能接收浮点值?

无论如何,这是我的python代码:

@app.route('/bprices', methods=['GET'])
def bprices():
    f = requests.get(
        'https://api.hypixel.net/skyblock/bazaar?key=[can provide another key IF NEEDED]').json()

    products = [
        {
            "id": product["product_id"],
            "sell_price": product["sell_summary"][:1],
            "buy_price": product["buy_summary"][:1],
            "sell_volume": product["quick_status"]["sellVolume"],
            "buy_volume": product["quick_status"]["buyVolume"],
        }
        for product in f["products"].values()
    ]
    return jsonify(products=products)

这是我的 HTML + 脚本:

  <h3>Products</h3>
  <table id="products_table">
    <thead>
      <tr>
        <th>Name</th>
        <th>Quantity</th>
        <th>Price</th>
      </tr>
    </thead>
  </table>
  <script src="/static/jquery3.5.js"></script>
  <script type=text/javascript>
        $SCRIPT_ROOT = {{ request.script_root | tojson | safe }};
        (function () {
            $.getJSON($SCRIPT_ROOT + "/bprices",
            function(data) {
                var products = data.products;
                var table_body = document.createElement("tbody");
                $.each(products, function(index, product){
                    var product_name = product.id.toString();
                    var product_quantity = product.sell_price;
                    var product_price = product.buy_price;
                    var row = table_body.insertRow();
                    var name_cell = row.insertCell();
                    name_cell.appendChild(document.createTextNode(product_name));
                    var quantity_cell = row.insertCell();
                    quantity_cell.appendChild(document.createTextNode(product_quantity));
                    var price_cell = row.insertCell();
                    price_cell.appendChild(document.createTextNode(product_price));
                })
                $("#products_table tbody").remove();
                $("#products_table").append(table_body);
            }
            );
            setTimeout(arguments.callee, 10000);
        })();
    </script>

谢谢 !:)

标签: javascriptpythonajaxflask

解决方案


推荐阅读