Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果输入文件在ajax请求中作为数组元素传递而没有表单,为什么数组对象变为空_Javascript_Jquery_Ajax_Form Data_Asyncfileupload - Fatal编程技术网

Javascript 如果输入文件在ajax请求中作为数组元素传递而没有表单,为什么数组对象变为空

Javascript 如果输入文件在ajax请求中作为数组元素传递而没有表单,为什么数组对象变为空,javascript,jquery,ajax,form-data,asyncfileupload,Javascript,Jquery,Ajax,Form Data,Asyncfileupload,当我在选择后尝试上载文件而不将其分配给数组时,它工作正常,但当将文件对象分配为数组的元素时$\u文件变为空 HTML <input type='file' name='image' class='image'> 注: 如果使用JSON.stringify(ajax_数据),则成功传递给服务器的所有数组元素都将变为空 虽然我不使用JSON.stringify()$\但文件将变为空 简单地说,我想将包含文件对象和纯字符串数据的数组传递给服务器,其中包括必要的数据。如果需要上传数组,您可

当我在选择后尝试上载文件而不将其分配给数组时,它工作正常,但当将文件对象分配为数组的元素时$\u文件变为空

HTML

<input type='file' name='image' class='image'>
注: 如果使用JSON.stringify(ajax_数据),则成功传递给服务器的所有数组元素都将变为空 虽然我不使用JSON.stringify()$\但文件将变为空


简单地说,我想将包含文件对象和纯字符串数据的数组传递给服务器,其中包括必要的数据。如果需要上传数组,您可以将
文件
对象转换为
数据URL
字符串,将数组转换为
JSON
并将
JSON
字符串上传到服务器。

为什么要上传JavaScript对象(数组)而不是
FormData
File
对象本身?问题代码处不使用
FormData
var img = $('.image').prop('files')[0];
var ajax_data=['product','price',img];

var postData = new FormData();
    postData.append('data',ajax_data);


$.ajax({
     type: "POST",
     processData: false, 
     contentType: false, 
     url: base_url + "/admin/create_bill",
     data:postData,
     success: function (data) {
                  alert('data has been submitted')
            }
 });
}
});