I have a problem with navigator.sendBeacon('endpoint/', data)
. I have an error Forbidden (CSRF token missing or incorrect.) because it pass data via POST method. I know i should set csrf token, but I don't know how set it in this case, anyone have any idea how it should be done? I'm using Django==2.2.6.
Asked
Active
Viewed 586 times
3

Piotr Kontowicz
- 31
- 2
1 Answers
1
You should do the following:
var data = new FormData();
data.append('csrfmiddlewaretoken', '{{csrf_token}}');
navigator.sendBeacon('endpoint/',data)
If it's a js file, then in your html you should create a variable like
<script>
var csrftoken = '{{ csrf_token }}';
</script>
Then you can use it when you append like this:
data.append('csrfmiddlewaretoken', csrftoken);

matebende
- 543
- 1
- 7
- 21