我有一个带有 method=post 的表单。在该表单中,我有一个 ajax 图像上传字段。当 ajax 调用过程时,验证发生 csrf 令牌不匹配错误。帮助我。这是我的代码..,
<script>
$(document).ready(function(){
$(document).on("click", "#upload", function() {
var file_data = $("#groupe_img").prop("files")[0]; // Getting the properties of file from file field
var form_data = new FormData(); // Creating object of FormData class
form_data.append("file", file_data) // Appending parameter named file with properties of file_field to form_data
form_data.append("csrftoken",document.mainform.csrftoken.value;) // Adding extra parameters to form_data
$.ajax({
url: "/upload_avatar",
dataType: 'script',
cache: false,
contentType: false,
processData: false,
data: form_data, // Setting the data attribute of ajax with …Run Code Online (Sandbox Code Playgroud)