小编典典

jQuery:一起发布FormData和CSRF令牌

django

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
        });
});

阅读 418

收藏
2020-03-30

共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-03-30