django 返回json数据,jquery获取并且打印出来
视图代码:
importjsondeftest(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}returnHttpResponse(json.dumps(data_dic))模板视图:defmoban(request):returnrender_to_response('moban.html')
django URL配置
url(r'^test/$',test),url(r'^moban/$',moban),
设置一个模板,模板内容如下:
<html><head><scripttype="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>向页面发送HTTPGET请求,然后获得返回的结果</button><p></p></body></html><!--#####################下面的方法也能获取到json数据,可以理解为模板2--><scripttype="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>向页面发送HTTPGET请求,然后获得返回的结果</button><p></p>
页面调试的时候建议使用火狐的firebug,很方便的!
最后附上一张效果图
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。