gpt4 book ai didi

javascript - 如何附加图像序列化?

转载 作者:行者123 更新时间:2023-11-30 13:59:32 26 4
gpt4 key购买 nike

如何在 Django 中将 image_form 添加到 form

表单 - form.serialize()

image_form - 图像

$('#id_submit').click(function(e) {
e.preventDefault();
var form = $('form').serialize();
image_form = $("#id_image")[0].files[0]

var data = {
form: form,
}

$.ajax({
headers: {'X-CSRFToken': '{{ csrf_token }}' },
type: 'POST',
data: data,
})
})

最佳答案

你可以这样尝试:

$('#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){}
});
});

关于javascript - 如何附加图像序列化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56589554/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com