Example 9
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$.ajax({
url:"{% url 'json' %}",
type:'POST',
data:{'id':1},
success: function (data) {
alert(data[0].id)
alert(data[0].name)
}
});
})
</script>
</body>
</html>
@csrf_exempt
def json(request):
if request.is_ajax or request.method == 'POST':
print(request.POST['id'])
data = [{
'id':'7788',
'name':'smith'
},
{
'id': '7788',
'name': 'smith'
}
]
return JsonResponse(data, safe=False )
This page should automatically redirect. If nothing is happening please use the continue link below.