django 返回json資料,jquery擷取並且列印出來

來源:互聯網
上載者:User

標籤:js   python   jquery   

視圖代碼:

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>


頁面調試的時候建議使用Firefox的firebug,很方便的!

最後附上一張

650) this.width=650;" title="2.png" src="http://s3.51cto.com/wyfs02/M00/53/CE/wKiom1RwfUfRCaz3AAEDa8Cs6J4859.jpg" alt="wKiom1RwfUfRCaz3AAEDa8Cs6J4859.jpg" />

django 返回json資料,jquery擷取並且列印出來

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.