0

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.