Django csrf in ajax POST (csrf cookie not set until {{csrf}} used)
问题 My django application uses ajax to add an item to shopping cart. The ajax request method is POST, and i enable request header via js: var csrftoken = getCookie('csrftoken'); $.ajaxSetup({ beforeSend: function (xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } } }); The problem is, that i send request not from the form, but just using a button and onClick event, so i do not use a {{ csrf }} in the template. So, the