温馨提示×

温馨提示×

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

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

django 返回json数据,jquery获取并且打印出来

发布时间:2020-07-21 21:06:32 来源:网络 阅读:785 作者:tianshuai369 栏目:web开发

视图代码:

import json
 
def test(request):
    pie_list = [[u'北京',50],[u'上海',20],[u'广州',30]]
    t_country = [u'北京',u'上海',u'广州']
    t_country_count = [50,20,30]
    data_dic = {'pie_g':pie_list,'t_country':t_country,'t_county_count':t_country_count}
     
    return HttpResponse(json.dumps(data_dic))
模板视图:

def moban(request):
    return  render_to_response('moban.html')

django URL配置

url(r'^test/$', test),
url(r'^moban/$', moban),

设置一个模板,模板内容如下:

<html>
<head>
<script type="text/javascript" src="/static/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $.get("/test/",function(data,status){
        $("p").append(data + "</br>");
    });
  });
});
</script>
</head>
<body>
<button>向页面发送 HTTP GET 请求,然后获得返回的结果</button>
<p></p>
</body>
</html>
<!--#####################下面的方法也能获取到json数据 ,可以理解为模板2-->
<script type="text/javascript" src="/static/js/jquery-1.10.1.min.js"></script>
<script>
$(document).ready(function() {
  $("button").click(function(){
    $.getJSON('/test/', { }, function (data) {
        $("p").append(data.pie_g + "</br>"  +data.t_country + "</br>"+ data.t_county_count)
    });
  });
});
</script>
<button>向页面发送 HTTP GET 请求,然后获得返回的结果</button>
<p></p>


页面调试的时候建议使用火狐的firebug,很方便的!

最后附上一张效果图

django 返回json数据,jquery获取并且打印出来

向AI问一下细节

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

AI