国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Django+Ajax+jQuery實現網頁動態更新

2019-11-10 21:07:05
字體:
來源:轉載
供稿:網友

views.py中的修改

增加相應的請求處理函數:

def getdevjson(request):    

返回字符串中,既可以使用from django.http import JsonResponse,也可以使用HttpResponse(json.dumps(res))

前端網頁修改

<script type="text/javascript">    window.jQuery || document.write("<script src='../static/js/jquery.min.js'>" + "<" + "/script>");</script><script type="text/Javascript">  $(function() {        var submit_form = function(e) {	 $.Ajax({            type : "GET",            url : "/getdevjson?"+Math.random(),            data : {                key: $('#searchContent').val()            },            dataType : "text",            success : function(res){			$('#searchContent').focus().select();			//console.log(res);            update(res);		 },			error : function() {                alert("處理異常返回!");}     		      });	          return false;    };    $('#calculate').bind('click', submit_form);    $('input[type=text]').bind('keydown', function(e) {      if (e.keyCode == 13) {        submit_form(e);      }    });    $('#searchContent').focus();  });</script>

<div class="divRight" id="divright1"> <div class="divRight" style="height:70px; width:370px;"><label id="lblSearch" class="CSSLabelSearch">請輸入查詢key:</label><input id="searchContent" type="text" size="40"></input> <input id="calculate" type="button" value="確定" ></input></div>  <br><label id="lbl1" class="cssLabelClient">節點信息</label><Textarea id="ClientInfoArea" readonly class="txtClientInfo"></Textarea></div>

#calculate是一個按鈕,點擊動作綁定了提交函數submit_form,ajax的請求參數中,data中包含了查詢參數,success是請求成功后的動作,注意返回的res需要進行json解析才可以正確使用:root = JSON.parse(jsondata);update(res)是一個更新網頁內容的函數

路由配置修改

urls.py中修改如下:

from django.conf.urls import patterns, include, urlfrom django.contrib import adminadmin.autodiscover()urlpatterns = patterns('',    url(r'^getdevjson$','dev.views.getdevjson',name='getdevjson'),    url(r'^','dev.views.index',name='index'),        url(r'^admin/', include(admin.site.urls)),)需要注意的是為了避免路由被覆蓋,將index的路由配置盡量放置在最后一行。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 营山县| 昌邑市| 洪江市| 闸北区| 岫岩| 谢通门县| 呼和浩特市| 满城县| 竹北市| 通渭县| 开平市| 福安市| 嘉义县| 鲁山县| 宣武区| 宜城市| 龙胜| 大悟县| 安仁县| 深水埗区| 陆良县| 顺平县| 邢台县| 深泽县| 疏勒县| 永德县| 揭西县| 正阳县| 马鞍山市| 鸡东县| 天津市| 巩义市| 宣化县| 饶平县| 镇宁| 当涂县| 邳州市| 庆安县| 伊吾县| 镶黄旗| 收藏|