response to ajax (jquery) with variables

102 views
Skip to first unread message

Antonio Russoniello

unread,
Aug 27, 2014, 8:56:12 PM8/27/14
to django...@googlegroups.com
Hello,

i hope you can help me with this, I'm trying to send to an ajax (jquery from my html template) a response but i would like to send also a new variable.

in my html template I have:

    $("#init_date").click(function(){
        var some_date = {'init_date': init_date, 'end_date': end_date};
        $.post("/filter_dates/", some_date, function(response){
            if(response === 'success'){
                MAKE SOMETHING!!!
            else{ alert('error'); }
        });
    });

my django view look like:

def filter_date(request):
    if request.method == 'POST':
        example = ['A','B','C']
        init_date = request.POST['init_date']
        end_date = request.POST['end_date']
         MAKE SOMETHING!!
        return HttpResponse('success') AND HERE I WOULD LIKE TO SEND THE example ARRAY...
    else:
        return HttpResponse('error')

How can I send the array o some other variable as response?

Regards,
AR

Andreas Kuhne

unread,
Aug 28, 2014, 3:28:23 AM8/28/14
to django...@googlegroups.com
Hi Antonio,

import simplejson as json

return HttpResponse(
    json.dumps({
        'array': example
    }),
    content_type="application/json"
)

That would do the trick. This is returned as JSON. Remember to set the content_type, otherwise the client can get confused.

Regards,

Andréas

--
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/53FE7E44.4020104%40musicparticles.com.
For more options, visit https://groups.google.com/d/optout.

Antonio Russoniello

unread,
Aug 29, 2014, 10:27:55 PM8/29/14
to django...@googlegroups.com
Thanks Andréas,

I will try it.

Regards/AR

Antonio Russoniello

unread,
Aug 29, 2014, 10:55:46 PM8/29/14
to django...@googlegroups.com
Now I can read data from  ajax to my def but I have not idea to how send, for examle, an array to jquery and manage this from javascript...

I tried:

def json_prueba(request):
    arrg = [1,2,3,4]
    if request.method == 'POST':
        return HttpResponse(simplejson.dumps({'array': arrg}), content_type="application/json")
    return HttpResponse('FAIL!!!!!')

from the page side:

    function updateDB(){
        $.post("/sitioweb/json/", data, function(response){
            if(response){
                var data = JSON.parse(response); <--------- NOT SURE IF THIS IS CORRECT
                alert(data[0]); }              <----- I´m trying to put on an alert windows the first item of my array arrg
            else{ alert('Error! :(');}
        });
    }

Thanks in advande.

AR


El 28/08/2014 02:57 a.m., Andreas Kuhne escribió:

Andreas Kuhne

unread,
Aug 30, 2014, 5:35:23 AM8/30/14
to django...@googlegroups.com
Hi again Antonio,

First of all, make sure you are using a good browser with debugging capabilities (I prefer Chrome), because you'll need it.

Secondly, I usually use the $.ajax request, instead of the post. Mainly because post is a wrapper around ajax and you can more finely change the settings of ajax.

So the post in your case would be:

$.ajax({
    type: 'POST',               // Make sure you post
    dataType: 'json',          // Use this so that you request json from the server
    url: "/sitioweb/json/",
    data: data,
    success: function(data) {
        // The data that is returned is your json data, already parser, so you don't have to parse it!
        alert(data.array[0]);     // Need to write "array" because that is what you are sending to the browser (your structure looks like that, check what the server is responding with)
    },
    error: function(xhr, textStatus, errorThrown) {
        console.log('AJAX communication failed:', textStatus, errorThrown);    // In case there is an error (for example a 500)
    }
});

Regards,

Andréas

Antonio Russoniello

unread,
Aug 31, 2014, 7:05:56 PM8/31/14
to django...@googlegroups.com
Thank you Andreas ... I got it !!!

AR
Reply all
Reply to author
Forward
0 new messages