JS+Python(ajax+json)例子

來源:互聯網
上載者:User

標籤:python   ajax   統計   

提供基於js與python利用ajax傳輸json資料的部分樣本:


在static檔案js目錄下:

    jquery.js

    st_goods.js


HTML頁面:

<!DOCTYPE html>{% load staticfiles %}<html><head>    <meta charset="utf-8">    <title>ECharts</title></head><body>    <div id="goods_statistics">商品統計</div>    <!-- 為ECharts準備一個具備大小(寬高)的Dom -->    <div id="main" style="height:400px"></div>    <!-- ECharts單檔案引入 -->    <script src="http://echarts.baidu.com/build/dist/echarts.js"></script>    <script src="{% static ‘js/jquery.js‘ %}"></script>    <script src="{% static ‘js/st_goods.js‘ %}"></script>    </script></body></html>


JS頁面

//商品統計$("#goods_statistics").click(function(){    $.ajax({        type: ‘get‘,        url: base_url + ‘/statistics/goods_st‘,        data : {},        success: function(e){        result = JSON.parse(e);        goods_st_sort(result.goods_st);        }    })});//商品名var g_name = [];//關注var marked = [];//收藏var collected = [];//購買var purchased = [];//瀏覽var browse = [];function goods_st_sort(goods_st){    var arr = new Array();    for(var i = 0 ; i < goods_st.length ; i++){        arr = goods_st[i].toString().split(‘,‘);        g_name.push(arr[0]);        //console.log(name[i]);        marked.push(arr[1]);        collected.push(arr[2]);        purchased.push(arr[3]);        browse.push(arr[4]);            }    draw();    //console.log(name.length);}


Python代碼

#使用者行為分析圖def goods_st_pic(request):   return render(request,website.goods_st_pic,None)#使用者行為資料def goods_st(request):   #串連資料庫   conn = MySQLdb.connect(host=‘120.26.38.125‘,user=‘root‘, passwd=‘passw0rd‘,db=‘jkbrother3‘,charset=‘utf8‘)   sql = "select distinct(g.name),                  g.marked_count,                  g.collected_count,                 g.purchased_count,                 (select sum(count) from adminer_access_record                  where mold =‘0‘ and action_id =g.id) browse_count           from adminer_goods g, adminer_access_record ar           where g.id=ar.action_id"   cur = conn.cursor()   #執行sql   cur.execute(sql)   #列出資料   records = cur.fetchall()   result = []   for row in records:       temp = (row[0],row[1],row[2],row[3],row[4])       result.append(temp)   conf = {‘goods_st‘:result}           #關閉串連   cur.close()   conn.close()   return HttpResponse(json.dumps(conf))   注意帶參數傳值,字典:var tdata = {"time":"2015-05-17"};//商品統計$("#traffic_statistics").click(function(){    $.ajax({        type: ‘post‘,        url: base_url + ‘/statistics/traffic_st‘,data : tdata,success: function(e){        result = JSON.parse(e);    print(result.traffic_st);        }    })});#網站流量統計def traffic_st(request):   time = request.POST["time"]   #串連資料庫


JS+Python(ajax+json)例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.