下面的data:..
行是否正确?我想将表单数据AND csrf标记发布到Django视图函数。
$('#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
});
});
答案 0 :(得分:12)
您必须将您的参数添加到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
});
});