[Answer]-Ajax response redirect

2πŸ‘

βœ…

I believe you need to do something like this:

$('#form').on('submit', function(event){
    event.preventDefault();
    $form = $('#form');
    $.ajax({
        type: $form.attr('method'),
        dataType: "JSON",
        url: $form.attr('action'),
        data: {
            form: $form.serialize()
        }
    }).done( function( response ) {
        console.log('ok');
        $( '#old' ).remove();
        $( '#contact' ).append(response.content);
    })
});

You need to prevent the form submission.

Another way to do this is maybe by using jQuery Form Plugin

Next thing you should do is to add CSRFToken to your Ajax header, more on this here

0πŸ‘

Make sure you are sending the csrfmiddlewaretoken with your ajax request if it’s a post. Otherwise you will get a 403 and possibly a redirect.

A quick way to test for this is to add the @csrf_exempt decorator to your view.

πŸ‘€Koed00

-1πŸ‘

Did you try this:

$( '#contact' ).html(response.content);

as you want to replace it?

πŸ‘€reyaner

Leave a comment