Home > Net >  Ajax Request to ApiView endpoint raises forbidden http error
Ajax Request to ApiView endpoint raises forbidden http error

Time:12-31

I have been trying to to an AJAX Request to an ApiView endpoint.

        $.ajax({
            url: '{% url 'edit_custom_user_additional_info' %}',
            method: "patch",
            data: $form.serialize(),
            header: {
                "X-CSRFToken": "{{ csrf_token }}"
            },
            success: function (data) {
                alert("Dati aggiuntivi salvati con successo");
            },
            error: function (data) {
                console.log("Errore durante il salvataggio dei dati aggiuntivi");
            },
        });

I have also added the {% csrf_token %} to the form but I still get Forbidden error. I have also tried to add the CSRF Exempt but still the error.

How can I fix that?

CodePudding user response:

I have added this to the ajax request and it worked

beforeSend: function(xhr) {
    xhr.setRequestHeader("X-CSRFToken", "{{ csrf_token }}");
}
  • Related