Javascript 使用来自数据库的Json数据填充下拉列表(使用Python FLASK)

Javascript 使用来自数据库的Json数据填充下拉列表(使用Python FLASK),javascript,html,json,Javascript,Html,Json,我将从PostgreSQL数据库检索到的JSON数据放入Flask函数中,该函数返回以下内容: 烧瓶功能: def Install(): cur = conn.cursor() # cur.execute("""create table Install (Install varchar(300));""") # cur.execute("""insert into Install (Install) values ('ffr2nightly.tst');""")

我将从PostgreSQL数据库检索到的JSON数据放入Flask函数中,该函数返回以下内容:

烧瓶功能:

def Install():
    cur = conn.cursor()
    # cur.execute("""create table Install (Install varchar(300));""")
    # cur.execute("""insert into Install (Install) values ('ffr2nightly.tst');""")
    cur.execute("""select * from Install;""")
    conn.commit()
    test = {}
    count = 0
    for row in cur.fetchall():
        test[count] = row[0]
        count += 1
    return test
输出:

{  
   "0": "ffr2nightly.tst",
   "1": "ffr2nightly.tst",
   "2": "HealthCheckDocker.tst",
   "3": "NoInstall.tst"
}
我正试图在我的HTML表单和下拉列表中使用这些数据

HTML:


是的,您只需要从JSON中提取值,然后使用jQuery将其解析为select元素

//你的数据 风险值数据={ 0:ffr2nightly.tst, 1:ffr2nightly.tst, 2:HealthCheckDocker.tst, 3:NoInstall.tst }; //对其进行迭代并附加到select Object.valuesdata.forEachfunctionele{ var opt=+ele+; $'sel'.appendopt; };
我在一个名为Install1的函数中有数据,并分配给一个名为test的变量。我如何调用这个函数或变量来代替var data={0:ffr2nightly.tst,1:ffr2nightly.tst,2:HealthCheckDocker.tst,3:NoInstall.tst};两种方法:将其设置为全局变量,或者将我的解决方案设置为可以将变量传递到的函数调用。一个快速更新Install1是服务器端的python函数,test是服务器端python函数中的变量。现在,你能解释一下,在你给frontend的脚本中,我如何将它作为数据获取吗?我对Flask或Python一无所知。如果您的目标是学习如何从Python或Flask传递数据,那么这属于一个单独的问题。这个问题的措词和标记使您能够访问前端的json数据。查看您的问题历史记录,似乎昨天已经有人告诉您如何执行此操作。也许你应该看看另一个问题中提供的解决方案。
<select multiple="multiple" id='lstBox3' class="col-md-5">
  <option>
    // I want the Json data in these options
  </option>
</select>