2

Using Ajax or xmlHttpRequest I want to download excel file from django backend. Backend creates file in memory and returns it to user.

According to this answer I should use for this xmlHttpRequest, but there is no info how to set csrf middleware token in post request.

I tried:

To set request.setRequestHeader like this: request.setRequestHeader('x-csrf-token, window.CSRF_TOKEN) - token is missing

and in data:

request.send({'csrfmiddlewaretoken': window.CSRF_TOKEN, 'req': 'ExportAllMessages'});

I can't find any working solution with Ajax.

Victoria
  • 21
  • 3

1 Answers1

0

The location of CSRF token varies between projects. Just look at the template of your page and find how it is saved. Potential locations are:

  • <meta> tag in <head>
  • <input> field of type="hidden" in the form
  • cookie
  • global variable, which is not the case in your situation
Biller Builder
  • 303
  • 3
  • 10