So I'm trying to write an ajax script that post data to django without having to leave the page. Currently it is on submit however eventually im going to put it on a timer.
Right now the function outputs to the console as a success but no data is returned from django.
Here is my ajax
<script type="text/javascript">
$(document).ready(function() {
$('#get_info').submit(function() {
$.ajax({
type: $('#get_info').attr('POST'),
url: $('#get_info').attr('action'),
data: $('#get_info').serialize(),
success: function (data) {
console.log('done');
console.log($('#get_info').serialize());
},
error: function(data) {
console.log('something went wrong');
}
});
return false;
});
});
</script>
Here is the serialized data it outputs to the console.
"done" vmstatus:34
"csrfmiddlewaretoken=HApMk9LWlxNUdlJaD4kj0WZkCTki4hin&selected_customer=24&user=True"
However no django data is returned after that? I know the django code works because I was using regular form submission to test it.
here is my views code:
if request.method == 'POST':
posted_data = request.POST.copy()
customer_id = int(posted_data['selected_customer'])
for customer in customers:
if customer.customer_id == customer_id:
selected_customer = ((decrypt('secretkey', customer.customer_name), int(customer.customer_id)))
customer_list.append((decrypt('secretkey', customer.customer_name), int(customer.customer_id)))
vms = PortalCommandCenter.objects.filter(customer_id = customer_id).distinct('vm_id')
context = {'user.is_superuser':user.is_superuser,
'customer_list':customer_list,
'customer_id':customer_id,
'vms':vms,
'posted_data':posted_data,
'selected_customer':selected_customer
}
return render(request, 'vmstatus.html', context)
do I need to serialize the data as json so that it is still a dictionary if so how do I do that?