【问题标题】:Flask, How to refresh an ajax url with a generated idFlask,如何使用生成的 id 刷新 ajax url
【发布时间】:2022-01-23 12:27:49
【问题描述】:

我是 javascript 新手,我创建了一个烧瓶站点,我想关注 ansible tower 的工作。 我已经创建了一个特定的路线:

@app.route("/tower/<int:id>", methods=['POST','GET'])
def status(id):
    launch = True
    job_info = {}
    status = refreshstatus(id)
    return render_template(
        'tower.html',
        job_info = job_info,
        status = status,
        launch = launch,
        id = id)

@app.route("/tower", methods=['POST','GET'])
def tower():
    launch = False
    if request.method == 'POST':
        keyword = request.form['launchjob']
        logger.info("Test | Keyword var => " + keyword)
        template_id = request.form['template_id']
        job_info = launch_job(template_id, keyword)
        launch = True
        return render_template('tower.html', job_info = job_info, launch = launch)
    else:
        return render_template('tower.html')

我的 js 脚本:

function refresh() {
    $.ajax({
        url: '/tower/' + id,
        dataType: 'json',
        id: { id : job_info.job_id },
        success: function(data) {
        $('#statustable').html(data);
        }
      });
      setTimeout(refresh, 10000);
      console.log('refresh')
    };
$(function(){
      refresh();
});

还有我的html文件

<th scope="row"></th>
<td> {{ job_info.job_id }}</td>
<td><p class="text-warning" id="statustable">{{ job_info.job_status }}</p></td>
<td><a href="{{ job_info.url }}" target="_blank">Lien vers le stdout</a></td>

当我手动刷新时,作业状态会发生变化,但不会自动刷新。 你能帮忙吗?

谢谢

大卫

【问题讨论】:

  • 你的JavaScript中的变量idjob_info.job_id是从哪里来的?
  • job_info.job_id 来自 python 函数 tower => job_info = launch_job(template_id, keyword),它是一个字典。并且 id 在 javascript 中声明:id: { id : job_info.job_id }
  • 不,不是。 JavaScript 在浏览器中运行,Python 在服务器上运行。他们看不到彼此的变数。所以我的猜测是,在您的浏览器控制台中有几个“变量未定义”错误。
  • 是的,你是对的,我看到变量未定义,但我该怎么做呢?

标签: javascript ajax flask


【解决方案1】:

JavaScript 看不到您服务器的变量。 job_info.job_id 不会在您的浏览器中定义。

如果你想在浏览器中使用它,你需要以某种方式将变量值传输到浏览器。

  • 一种方法是从&lt;td&gt;{{ job_info.job_id }}&lt;/td&gt; 读取文本。
  • 更好的方法是使用HTML data attribute

如果你在服务器端像这样改变你的视图:

<tr data-job-id="{{job_info.job_id|tojson|forceescape}}">
  <th scope="row"></th>
  <td>{{ job_info.job_id }}</td>
  <td><p class="text-warning" id="statustable">{{ job_info.job_status }}</p></td>
  <td><a href="{{ job_info.url }}" target="_blank">Lien vers le stdout</a></td>
</tr>

然后您可以在客户端使用 jQuery 访问来自 data-job-id="..." 的值:

function refresh() {
    const id = $('#statustable').closest("tr").data('job-id');
    $('#statustable').load('/tower/' + id);
    setTimeout(refresh, 10000);
};

$(refresh);
  • Jinja's tojson filter 一起,数据属性允许您将复杂数据(如完整的job_info 结构)传输到您的客户端,而不仅仅是像单个ID 这样的简单值。警告:记得使用forceescape 过滤器来避免issues
  • data-job-id="..." 移至 HTML 中最有意义的元素。
  • $(element).load(url)$.ajax(url) -> success -> $(element).html(response) 的缩写形式。见https://api.jquery.com/load/
  • $(refresh)$(function () { refresh(); }) 的缩写形式。见https://api.jquery.com/jquery/#jQuery3

【讨论】:

  • 它工作,我有一些设计要做,但 js 工作。谢谢你的时间,我会看看数据属性很有趣!
猜你喜欢
  • 2023-03-28
  • 2016-01-01
  • 2014-04-23
  • 2020-01-29
  • 2019-02-08
  • 2012-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多