Francisco Ghelfi
Francisco Ghelfi

Reputation: 962

Django: send array with AJAX

I want to send an array of values via AJAX to a Django view but I only find how to send forms.

For example I send the following array of values. How do I access that array in my views?

Let to_save = []

$.each($('.linea_producto').find('.esta_coleccion.elegido'), function(index, value) {
     producto = $(this).find('.producto').text();
     marca = $(this).find('.marca').text();
     packaging = $(this).find('.packaging').text();
     categoria = $(this).find('.categoria ').text();

     to_save.push({marca, producto, packaging, category});
});

$.ajax({
            url:'/target_url/',
            type:'POST',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify(to_save),

            success:function(response){

            },
            error:function(){

            },
        });

Thanks in advance!

Upvotes: 0

Views: 22

Answers (1)

tim-mccurrach
tim-mccurrach

Reputation: 6815

You can access the data you have sent in the body of your POST request via request.POST like so:

def my_view(request):
    post_data = request.POST
    # do something with post_data

request.POST will be a dictionary-like object.

Upvotes: 1

Related Questions