使用 jquery 发送 base64 编码图像列表

问题描述

当我向 node.js 服务器发送我的 ajax 请求时,包含我的图像列表的参数没有出现。 (我将服务器上的请求修改为 50mb) 这是我的 ajax 请求:

$.ajax({
                url: '/post_vehicle',type: "POST",Traditional: true,data: {
                    images: list,// here is the problem,this var does not arrive on the server
                    brand: $('.post_brand :selected').text(),model: $('.post_mark :selected').text(),location: $('.post_location :selected').text(),year: $('.post_year :selected').text(),fuel_type: $('post_fuel_type :selected').text(),}
            })

我在 ajax 之前使用了 console.log 来检查列表是否有图像,并且它向我显示了所有图像。

这是ajax中列表的值: List value

解决方法

我放弃将图像保存为 base64,而是像这样使用 FormData:

let formData = new FormData($('#sell_form')[0]); 
// #sell_form is the form id where i have all the data that i need.
//Now formData variable will contain all the values from your form inputs

Ajax 请求

$.ajax({
            url: '/post_vehicle',type: 'POST',data: formData,dataType:'json',processData: false,contentType: false,});

如果你想添加额外的数据

formData.append("Key",'Value');