Django and Ajax, Django not returning posted data.

98 views
Skip to first unread message

G Z

unread,
Nov 5, 2014, 1:33:42 PM11/5/14
to django...@googlegroups.com
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?

G Z

unread,
Nov 5, 2014, 1:34:48 PM11/5/14
to django...@googlegroups.com
Another note is I try to return posted data and out put it to the html for testing and that doesn't get returned either.

G Z

unread,
Nov 5, 2014, 2:15:01 PM11/5/14
to django...@googlegroups.com
updated my ajax to the following but for somereason it just says something went wrong


<script type="text/javascript">
$
(document).ready(function() {
    $
('#get_info').submit(function() {
        $
.ajax({

                type
: "POST",
                url
: "/vmstatus/",
                dataType
: "json",
                async
: true,
                data
: {
                    csrfmiddlewaretoken
: '{{csrf_token}}',
                    selected_customer
: $('selected_customer').val()
               
},
                success
: function (json) {
                    console
.log('done');
                    setInterval
(returnData, 5000);  
                    console
.log(json);
               
},
                error
: function(json) {

                    console
.log('something went wrong');

                    console
.log(json);

               
}
           
});
       
return false;
   
});
});
   
var returnData = function () {
    $
.ajax({
                type
: "POST",
                url
: "/vmstatus/",
                dataType
: "json",
                async
: true,
                data
: {
                    csrfmiddlewaretoken
: '{{csrf_token}}',
                    selected_customer
: $('selected_customer').val()
               
},
                success
: function (json) {
                    console
.log('done');
                    console
.log(json);
               
},
                error
: function(json) {

                    console
.log('something went wrong');

                    console
.log(json);
               
}
           
});
};  
   
   
</script>


Collin Anderson

unread,
Nov 12, 2014, 3:15:28 PM11/12/14
to django...@googlegroups.com
Hello,

In your first method (non-json), try console.log(data) in the success function.

When you tried json, was there any other error in the console? What about the network tab?

Collin

Reply all
Reply to author
Forward
0 new messages