您的位置:首页 > Web前端 > JavaScript

ajax接收django发送的json数据

2013-04-25 22:41 856 查看
首先django要有写发送json数据的方法

import simplejson

def sendjson(request):
''' 发送json数据 '''
result = {'username': 'username', 'password': 'password'}
result = simplejson.dumps(result)

return HttpResponse(result)

def index(request):
''' 渲染sendjson.html模版页面 '''
render_to_response('sendjson.html', locals())


sendjson.html

<html>
<body>

<script type='text/javascript'>
$(function(){
$.ajax({
type: 'POST',                         // 发送请求的方式
url: '{% url sendjson %}',             // 发送请求
dataType: 'json',                 //  返回数据的格式
success: function(data){              // data 为json数据
$('#recive').html(data)
},
});
});
</script>

<div id='receive'></div>

</body>
</html>


用json传数据的时候在数据量比较大的时候还是很爽的

jquery中有个方法是getJson()。是ajax函数的缩写。相当于:

$.ajax({
dataType: 'json',
url: url,
data: data,
success: success,
})


每天一点点收获...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐