JSON Response

34 views
Skip to first unread message

elcaiaimar

unread,
Feb 19, 2015, 2:14:32 PM2/19/15
to django...@googlegroups.com
Hello,

I was wondering how I can send a response JSON in a good way, because I have the next code:

if "product_id" in request.POST:
            try:
                id_producto = request.POST['product_id']
                p = Pozo.objects.get(pk=id_producto)
                mensaje = {"status":"True","product_id":p.id}
                p.delete() # Elinamos objeto de la base de datos
                return JsonResponse(mensaje)
            except:
                mensaje = {"status":"False"}
                return JsonResponse(mensaje)

And my JS function doesn't work with this code:

   var options = {
        success:function(response)
        {
            if(response.status=="True"){
                alert("Eliminado!");
                var idProd = response.product_id;
                var elementos= $(nombre_tabla+' >tbody >tr').length;
                if(elementos==1){
                    location.reload();
                }else{
                    $('#tr'+idProd).remove();
                    $(nombre_ventana_modal).modal('hide');
                }
            }else{
                alert("Hubo un error al eliminar!");
                $(nombre_ventana_modal).modal('hide');
            };
        }
    };
    $(nombre_formulario_modal).ajaxForm(options);
});

And I don't know what I can do. Does anyone have any idea?

Vijay Khemlani

unread,
Feb 19, 2015, 3:49:59 PM2/19/15
to django...@googlegroups.com
What do you mean with "in a good way"? Does your code work?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2cfc6283-ee2f-4280-b086-2dc1595855c8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

elcaiaimar

unread,
Feb 19, 2015, 5:06:59 PM2/19/15
to django...@googlegroups.com
I mean in a correct form. My code doesn't work. It seems like the JS code doesn't receive the jsonresponse.
I get a page with {"status":"True","product_id":p.id} But this should be read for the JS code, and if it's True show an alert saying Remove it!

Is there anything wrong in my code?

Vijay Khemlani

unread,
Feb 19, 2015, 7:03:54 PM2/19/15
to django...@googlegroups.com
What is the actual content of the respose you are getting back from the server?

Collin Anderson

unread,
Feb 20, 2015, 12:25:52 PM2/20/15
to django...@googlegroups.com
Hi,

It sounds like you click "submit" and then get a page showing the raw json? That sounds like it could be a bug in your code. Either the event handler is not connected, or there's an error. You could try checking "Preserve Log" in the developer console.

Collin

Daniel Roseman

unread,
Feb 20, 2015, 2:03:38 PM2/20/15
to django...@googlegroups.com
What it sounds like actually is that the OP is not preventing the default action of the form, so the page is being submitted normally and displaying the response.
--
DR.

elcaiaimar

unread,
Feb 20, 2015, 3:01:46 PM2/20/15
to django...@googlegroups.com
Please, have a look at this tutorial: https://www.youtube.com/watch?v=Pxn5L1qJTAw
Sorry, It's in spanish, but it's what I'm doing. I've tried to put the same code but it doesn't work because he uses an older version of django.
If you don't want to see all the video, you can see the minute 12:16.
He clicks on the remove button and then, an alert appears!
My django version is 1.7

I hope this helps!

And Thank you for your answers!

elcaiaimar

unread,
Feb 20, 2015, 7:49:38 PM2/20/15
to django...@googlegroups.com
I solved it! Sorry, at last I had a bad called to jquery library in my base.html. I had to check all my project
Thank you for your answers, you have helped me a lot!
Reply all
Reply to author
Forward
0 new messages