javascript - 当使用 AJAX 从烧瓶返回值时,它显示 [Object, Object]
问题描述
所以我试图让我的项目通过 API 实时更新价格。我设法做到了,但价格本身是浮点值(int 和字符串工作得很好!)。我跑过去看看可能是什么问题,上面说console.log
和我回来了。print()
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>
谢谢 !:)
解决方案
推荐阅读
- linux - 将 Powershell 中的变量扩展为 Linux 命令
- discord - 使用 Discord 的 API 以用户身份发送消息
- python - 我从头开始构建 matplotlib 中的烛台图有对齐问题
- javascript - 反应:意外的令牌,预期的“,”在 JSX 中
- python - 使用 BeautifulSoup 为抓取的网页的 Script 标签提取所需信息
- java-native-interface - 使用 protobuf-cpp 作为 JNI 的依赖项时出错
- javascript - 如何在 node js 中为 route.get 设置标头和参数?
- sql - 需要提取在 2 个表之间具有相同列名的行,并且只显示第 2 个表的公共行
- html - 文本和按钮并排
- google-apps-script - appendRow() 不可靠(覆盖“重”电子表格中的现有值/行)