当使用AJAX从烧瓶返回值时,它说[Object,Object]

时间:2020-06-03 14:05:21

标签: javascript python ajax flask

因此,我试图通过API来实现我的项目更新价格。我设法做到了,但是价格本身就是浮点值(整数和字符串都很好!)。我跑了console.logprint()来查看问题所在,在print()上说<class 'dict'>,在console.log上我回来了Object

我想我必须进行一些更改才能接收浮点值?

无论如何,这是我的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>

谢谢!:)

0 个答案:

没有答案