【问题标题】:Reloading Variable Value when the value is updated from Server (FLASK, HTML, JQUERY)从服务器(FLASK、HTML、JQUERY)更新值时重新加载变量值
【发布时间】:2020-06-16 13:22:40
【问题描述】:

我对 python 非常陌生,并且开始学习使用 AJAX 和 Jquery。 我将尝试清楚地阐述我的问题,但如果有任何不清楚的地方,请告诉我,我会努力改进它。

我有一个从函数 userinterface.py 运行的网络应用程序,而 userinterface.py 应该从函数 central.py 运行。

在central.py中,有一个叫“devicequantity”的变量,应该赋值给html中的一个div元素。我尝试对此使用 ajax 请求(devicequantity 是一个不断变化的变量)。

在我的网络应用程序中,根据 devicequantity 的值隐藏了一些字段。如何将 devicequantity 分配给 Web 应用程序,并在通过 central.py 分配新值时让它更新?非常感谢您的任何反馈和帮助!

这是我的代码:

html

<div class="col-sm-4 d-inline-flex justify-content-center align-items-center my-0">
  Device quantity:
  <div class="border col-sm-5 px-2 py-2 justify-content-center align-items-center" type="number" id="devicequantity" name="devicequantity"> {{ devicequantity }} </div>
</div>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

<script>
$(document).ready(function(){
    $.ajax({
      url:"/getdevicequantity",
      type: "get",
      success: function(response){
        $("#devicequantity").html(response.devicequantity);
      }
    });
    $('#alertsettings').hide(); $('#alertfillfield').hide(); $("#device1").hide(); $("#device2").hide(); $("#device3").hide(); $("#device4").hide(); $("#device5").hide(); $("#device6").hide(); $("#device7").hide(); $("#device8").hide(); $("#device9").hide();$("#device10").hide(); $("#SubmitSecondPart").hide(); $("#alertquantity").show();
    if ($("#devicequantity").val() == 1) {$("#alertquantity").hide(); $("#device1").show(); $("#SubmitSecondPart").show();
    }
    else if ($("#devicequantity").val() == 2) {$("#alertquantity").hide(); $("#device1").show(); $("#device2").show(); $("#SubmitSecondPart").show();
    }
    else if ($("#devicequantity").val() == 3) {$("#alertquantity").hide(); $("#device1").show(); $("#device2").show(); $("#device3").show(); $("#SubmitSecondPart").show();
    }
});
</script>
</script>

userinterface.py

@app.route("/", methods=['GET', 'POST'])
def home():
   if request.method == 'POST':
      getInputsSettings()
      getInputsLicenses()
   return render_template('printerlabel.html', devicequantity=devicequantity)

@app.route("/getdevicequantity")
def getdevicequantity():
   return jsonify({'devicequantity' : devicequantity})

def runuserinterface():
   app.run()

if __name__ == "__main__":
   app.run()

central.py

devicequantity = 10
if __name__ == "__main__":
    userinterface.devicequantity=devicequantity
    userinterface.runuserinterface()

【问题讨论】:

    标签: python jquery flask


    【解决方案1】:

    您的 AJAX 成功回调函数有一个您需要使用的响应参数。

    它应该看起来像这样:

    success: function(response){
        $("#devicequantity").html(response.devicequantity);
    }
    

    出于调试目的,您可以尝试

    success: function(response){
        console.log(response);
    }
    

    【讨论】:

    • 如果它不起作用,您可能需要解析 JSON。 .html(JSON.parse(response). devicequantity)
    • 嘿!尝试应用您的建议,但仍然无法正常工作。 devicequantity 不会更新,也无法获取 $("#devicequantity").val()... 非常感谢您这么快回答!
    • 尝试控制台日志response,你会看到它是否真的返回了一个值或者你有其他问题
    • 控制台显示成功,但是当 central.py 中的新值被保存时,webapp 上的 devicequantity 仍然没有得到更新。这可能与我从 central.devicequantity 分配 userinterface.devicequantity 的值有关吗?
    • 如果您提供该回复的屏幕截图,我会更有帮助
    猜你喜欢
    • 2018-02-10
    • 2018-09-10
    • 2019-06-25
    • 1970-01-01
    • 2018-06-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多