Django jsonresponse和ajax

时间:2012-03-27 14:04:43

标签: ajax json django jsonresponse

我正在尝试使用以下代码从django发送JSON响应:

def get_symptoms(request):
    reply = simplejson.dumps({"name":"Amit","address":"B-123 Bangalow","age":23,"phone" :"011-4565763"})
    return HttpResponse(reply, mimetype="application/json")

在客户端下面的代码:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<script>
$.getJSON('http://127.0.0.1:8000/showsym/', function(data) {var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
 });

 $('<ul/>', {'class': 'my-new-list', html: items.join('') }).appendTo('body');});
</script>

</body>
</html>

想要解析此JSON对象并以html格式表示它。 上面的代码给出了一个包含jsonresponse字符串的文件。

在urls.py中我正在使用以下网址:

  url(r'^showsym/$', 'amoha.symptoms.views.get_symptoms', name='home'),

0 个答案:

没有答案