如果我保存从json扩展文件中的视图函数抛出的json它可以工作,但我无法直接从我的视图函数url中获取数据。 我甚至尝试将html文件包含为模板并使用新视图渲染它并使用{%url:thrower%}标记,但它也没有工作
下面的函数抛出数据
def thrower(request):
query=Category.objects.all()
query=serializers.serialize('json',query)
return HttpResponse(query,content_type="application/json")
url
url(r'^thrower/$', views.thrower, name='thrower')
My jquery function and html
$(document).ready(function () {
$('#get-data').click(function () {
$.getJSON('/thrower',
function(data) {
// data is [{},{},{}]
$.each(data, function(key, val) {
// val is { pk, model, {fields}}
$("#show-data").append("Object id is: " + val.pk + ' of model: ' + val.model);
$.each(val.fields, function(fieldname, field) {
$("#show-data").append(fieldname + ' : ' + field);
});
});
}, "json");
});
});
html file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Request JSON Test</title>
</head>
<body>
<a href="#" id="get-data">Get JSON data</a>
<div id="show-data"></div>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="example.js"></script>
</body>
</html>
json from thrower function
[{"model": "bhu.category", "pk": 1, "fields": {"name": "Test"}},
{"model": "bhu.category", "pk": 2, "fields": {"name": "test2"}}, {"model": "bhu.category", "pk": 3, "fields": {"name": "test3"}}, {"model": "bhu.category", "pk": 4, "fields": {"name": "deepak"}}, {"model":