问题是将 javascript arraylist 与 RequestParam 注释绑定(bind)。 如果没有 nokDataList,我有一个这样的表单数据,效果很好。 nokInfoDatas 是一个 JavaScript 数组,在控制台中它看起来如下:
var requestData = JSON.stringify(nokInfoDatas);
console.log("nokInfoDatas");
console.log(nokInfoDatas);
console.log("requestData");
console.log(requestData);
var fd = new FormData();
fd.append('photo', file);
fd.append('testId', testId);
fd.append('nokDataList', nokInfoDatas);
var ajaxData = {
type: "POST",
data: fd,
processData : false,
contentType : false,
url: sendUrl,
headers: headersData,
};
后端:
public @ResponseBody String answer(HttpServletRequest request,
@RequestParam(value = "photo") MultipartFile photo,
@RequestParam(value = "testId") String testId,
@RequestParam(value = "nokDataList") List<NokDataDTO> nokInfoDtos
)
最佳答案
您可以尝试如下:
创建 Blob与您的 JSON 数据 (requestData) :
var requestData = JSON.stringify(nokInfoDatas);
var fd = new FormData();
fd.append('photo', file);
fd.append('testId', testId);
fd.append('nokDataList', new Blob([requestData], {
type: "application/json"
}));
将@RequestParam更改为@RequestPart用于解析多部分 JSON。
public @ResponseBody String answer(HttpServletRequest request,
@RequestParam(value = "photo") MultipartFile photo,
@RequestParam(value = "testId") String testId,
@RequestPart(value = "nokDataList") List<NokDataDTO> nokInfoDtos
)
关于javascript - 如何使用 FormData 将 javascript 数组与 @RequestParam 绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47901304/