Django 怎样传数据给HTML页面的ajax
1个回答
展开全部
Django传数据给HTML页面的ajax的方法是通过load方法传入的。
举例:ajax.html
<html>
<head>
<meta charset="utf-8" />
<title>ajax test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery(function($){
$('button').click(function(){
$name = $(this).attr('name');
$('#out').empty().load('/ajax_deal/',{ name : $name });
});
});
</script>
<style type="text/css"></style>
</head>
<body>
<button id="btn-1" name="1">1</button>
<button id="btn-2" name="2">2</button>
<button id="btn-3" name="3">3</button>
<div id="out"></div>
</body>
</html>
设置下路由:url(r'^ajax_deal/$','jobs.views.ajax_deal'),创建名为jobs的app
/opt/django/webproject/jobs
处理函数:
def ajax_deal(request):
return HttpResponse("hello")
举例:ajax.html
<html>
<head>
<meta charset="utf-8" />
<title>ajax test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery(function($){
$('button').click(function(){
$name = $(this).attr('name');
$('#out').empty().load('/ajax_deal/',{ name : $name });
});
});
</script>
<style type="text/css"></style>
</head>
<body>
<button id="btn-1" name="1">1</button>
<button id="btn-2" name="2">2</button>
<button id="btn-3" name="3">3</button>
<div id="out"></div>
</body>
</html>
设置下路由:url(r'^ajax_deal/$','jobs.views.ajax_deal'),创建名为jobs的app
/opt/django/webproject/jobs
处理函数:
def ajax_deal(request):
return HttpResponse("hello")
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询