我似乎遇到了一个FormData为空的问题。我试图在单个POST请求中上传文件和JSON。我尝试了各种方法,但似乎都没有起作用。我想知道是否我在这里搞错了一些基本的东西,但我找不到任何问题。非常感谢你的帮助!
<form id="uploadCSVWithData" method="post" enctype="multipart/form-data">
<div class="fileUpload btn btn-info">
<i class="fa fa-cloud-upload" aria-hidden="true"></i>Upload CSV File <input type="file" name="csvUploadFile" class="upload" />
</div>
<input type="text" class="form-control" name="distName" id="distName"></br>
<input type="text" class="form-control" name="columnNames" id="columnNames"></br>
<input type="text" class="form-control" name="columnPrices" id="columnPrices"></br>
<input type="text" class="form-control" name="columnCases" id="columnCases"></br>
<input type="text" class="form-control" name="columnIds" id="columnIds">
</form>
还有JavaScript:
event.preventDefault();
var prices = $("#columnPrices").val();
var id = $("#columnIds").val();
var names = $("#columnNames").val();
var distName = $("#distName").val();
var cases = $("#columnCases").val();
var json = {
columnPrices: prices,
columnIds: id,
columnNames: names,
columnDistName: distName,
columnCases: cases
}
var data = new FormData($("#uploadCSVWithData"));
console.log("Data is " + JSON.stringify(data))
$(this).html('<i class="fa fa-spinner fa-spin" aria-hidden="true">');
$.ajax({
type: "POST",
url: '{removed}',
data: {
json: json,
formData: data
},
success: function(done) {
$("#submitCSVForUpload").html("Submit");
$("#uploadFromCSVModal").modal('hide');
},
processData: false,
contentType: false
});
console.log(...data);
- shaedrich