【发布时间】:2018-11-20 21:53:49
【问题描述】:
我发现这看起来非常接近我想要实现的目标。这是我的第一个 Python 项目,我对 Ajax 一无所知,所以我不确定我在看什么:
Execute python script -- Ajax and Flask
我得到第一个带有 Flask 路线的盒子 - 我正在这样做,但有很多变量。
我得到第二个带有 html 的框,显示从 Flask 路由传递的变量。
这是我正在努力解决的最后一个盒子。
在我的模板中我有:
<div id="insidetemp">
<i class="fas fa-thermometer-half"></i> {{ intTemp }}℃
</div>
<div id="heatingtarget">
<i class="fas fa-fire"></i> {{ targetTemp }}℃ <i class="far fa-clock sm"></i>
</div>
这些都是我的/路由生成的。
他有:
function cputemp2() {
$.ajax({
type: "POST",
url: "/cputemp",
dataType: "html",
success: function(msg) {
console.log(msg);
$("#swiss").html(msg);
},
error: function (xhr, status, error) {
console.log(error);
}
});
}
所以,我想我可以更改函数的名称。我会将 URL 更改为 /。我会将 id 更改为 #insidetemp 以定位模板中的 div。
我要先试一试:
function syncvalues() {
$.ajax({
type: "POST",
url: "/",
dataType: "html",
success: function(msg) {
console.log(msg);
$("#insidetemp").html(msg);
},
error: function (xhr, status, error) {
console.log(error);
}
});
}
但是如何添加更多变量?如果有人可以让我开始使用 2 个变量,那么我希望我可以将其扩展到 10 个。
【问题讨论】:
标签: python ajax templates flask