如何使用jquery动态刷新json数据

发布时间:2021-10-19 09:59:02 作者:小新
来源:亿速云 阅读:166

这篇文章将为大家详细讲解有关如何使用jquery动态刷新json数据,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

有个功能:web上需要定时刷新后台数据

解决方案:数据通过json传给js,设置定时器进行页面局部刷新处理

代码:

#django后台代码
@csrf_exempt
@login_required
def deploy_json(request, id):
    with open("/data/shell/autodeploy.log", "r") as data:
        lines = len(list(enumerate(data)))
        if int(id)<=lines:
            print lines
            jsondata = json.dumps([{"subject" : linecache.getline("/data/shell/autodeploy.log",int(id)), "count" : lines}], ensure_ascii=False)
            print jsondata
            response = HttpResponse()
            response['Content-Type'] = "text/javascript"
            response.write(jsondata)
            return response
            data.close()
        else:
            return HttpResponse()
            data.close()

@csrf_exempt
@login_required
def auto_deploy(request):
    if request.method == 'POST':
        subcmd.Jso_out("sh /data/shell/autodeploy.sh | tee /data/shell/autodeploy.log")
        return render_to_response("deploy/auto_deploy.html")
    elif request.method == 'GET':
        return render_to_response("deploy/auto_deploy.html")
   

urls.py中添加如下:
url(r'^deploy/(?P<id>\d+)/$',deploy_json),
#html代码
{% extends "default/base.html" %}
{% block title %} 发布系统 {% endblock %}

{% block content %}
<body class="pageBg">
    <h4 class="w_1320 pageTitle">
        <span>发布系统</span>预发布环境一键发布
    </h4>
</body>

<form method="POST" action="/deploy/pre_auto">
{% csrf_token %}
  <p align="left" class="style5">
      <input type="submit" class="pageTitle_input01" value="发布(预发布)">
</form>
<pre class="pre-scrollable prettyprint linenums Lang-bash">
 输出信息:
<script type="text/javascript" language="javascript">
var i = 0;
function update(id) {
   $.getJSON("/deploy/" + id + "/",function(data) {
         $("#content").append('<p>' + data[0].subject + '</p>');
         console.log(id,Number(data[0].count));
         if(id >= Number(data[0].count)){
          clearInterval(time);
         }
   });
}

var time = setInterval(function() {
    i += 1;
    update(i)
     }, 1000);

$(document).ready(function() {
{#   timeDown(3, 0) #}
    update(0)
})
</script>
<p id="time"></p>
<div id="content"></div>
</pre>
{% endblock %}

效果:

如何使用jquery动态刷新json数据

关于“如何使用jquery动态刷新json数据”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. jquery easyui combobox 下拉框组件动态刷新取值
  2. jquery获取json数据

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jquery json

上一篇:怎么正确设计一个订单号

下一篇:用D语言写脚本的原因有哪些

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》