L
L
leo97972020-05-24 14:05:36
Django
leo9797, 2020-05-24 14:05:36

What to do with CSRF token missing or incorrect?

I want to send recorded audio file to View using AJAX. Writes a message:

Forbidden (CSRF token missing or incorrect.)


The jQuery code itself:
console.log("Остановка записи")
rec.stop()
gumStream.getAudioTracks()[0].stop();
$('#btnToStopRecord').attr('id', 'btnToRecord');
$("#toMessage").prop('disabled', false).attr('placeholder', "Введите сообщение...")
rec.exportWAV(function (blob) {
    let data = new FormData()
    data.append('csrfmiddlewaretoken', '{{ csrf_token }}')
    data.append('audio', blob)
    console.log(data)
    $.ajax({
        type: "POST",
        url: '{% url 'sendtext' %}',
        contentType: false,
        cache: false,
        processData: false,
        data: {
            'audioFile': data,
             'csrfmiddlewaretoken': '{{ csrf_token }}',
             },
      })


I also used in the view.py file:
  • @csrf_exempt
  • @method_decorator(csrf_protect)


Still nothing, tell me what to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dima Polos, 2020-05-24
@leo9797

You need to get a csrf token from the backend, and then send it back to the backend from the client along with a request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question