JQuery: post FormData AND csrf token together

后端 未结 1 1828
庸人自扰
庸人自扰 2020-11-30 10:38

Is the data:.. line below correct? I want to post the form data AND csrf token to a Django view function.

$(\'#file-upload\').on(\'change\', fu         


        
相关标签:
1条回答
  • 2020-11-30 11:10

    You have to add your parameters to the FormData object (using append) and as always pass the formdata object alone as the data property.

    $('#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
            });
    });
    
    0 讨论(0)
提交回复
热议问题