如何附加图像进行序列化?

时间:2019-06-13 23:07:52

标签: javascript jquery django ajax serialization

如何在image_form的{​​{1}}上添加form

form-form.serialize()

image_form-图片

Django

1 个答案:

答案 0 :(得分:2)

您可以尝试:

$('#id_submit').click(function(e) {
    e.preventDefault();
    var form = $('form').serialize();
    var image_form = $("#id_image").prop("files")[0];
    var data = new FormData(); // Creating object of FormData class
    data.append("form", form);
    data.append("image_form", image_form);
    $.ajax({
      headers: {'X-CSRFToken': '{{ csrf_token }}' },
      type: 'POST',
      data: data,
      //Options to tell JQuery not to process data or worry about content-type
      contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
      processData: false, // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
      cache: false, // To unable request pages to be cached
      success: function (res){}
   });
 });