小编典典

jQuery:一起发布FormData和CSRF令牌

ajax

data:..下面的线正确吗?我想将表单数据和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
        });
});

阅读 228

收藏
2020-07-26

共1个答案

小编典典

您必须将参数添加到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
        });
});
2020-07-26