data:..下面的线正确吗?我想将表单数据和csrf令牌发布到Django视图函数中。
data:..
$('#file-upload').on('change', function () { var currentpath = window.location.pathname; var formData = new FormData($('form')[0]); $.ajax({ url: currentpath, //server script to process data type: 'POST', data: {formData, 'csrfmiddlewaretoken': '{{ csrf_token }}'}, cache: false, contentType: false, processData: false }); });
你必须将参数添加到FormData对象(使用append),并且一如既往地将formdata对象作为data属性单独传递。
$('#id_image').on('change', function () { var currentpath = window.location.pathname; var formData = new FormData($('form')[0]); formData.append('csrfmiddlewaretoken', '{{ csrf_token }}'); $.ajax({ url: currentpath, //server script to process data type: 'POST', data: formData, cache: false, contentType: false, processData: false }); });