我需要在ajax调用中同时传递FormData和JSON对象,但是却收到400 Bad Request错误。
[artifact:mvn] org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn] at [Source: java.io.PushbackInputStream@3c0d58f6; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value
[artifact:mvn] at [Source: java.io.PushbackInputStream@3c0d58f6; line: 1, column: 3]
JS:
var formData = new FormData(form[0]);
//form JSON object
var jsonData = JSON.stringify(jcArray);
$.ajax({
type: "POST",
url: postDataUrl,
data: { formData:formData,jsonData:jsonData },
processData: false,
contentType: false,
async: false,
cache: false,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success: function(data, textStatus, jqXHR) {
}
});
控制器:
@RequestMapping(value="/processSaveItem",method = RequestMethod.POST")
public @ResponseBody Map<String,String> processSaveItem(
@RequestBody XYZClass result[])
}
有一个类似的问题,jQuery在ajax调用中发送表单数据和json对象,而我正在尝试相同的方法。
如何在单个ajax请求中同时发送FormData和JSON对象?
我通过在Ajax POST中将JSON字符串与FormData对象附加在一起来解决。
var jsonObjectData = JSON.stringify(jcArray);
formData.append("jsonObjectData",jsonObjectData);
在控制器中,您可以像处理其他表单数据值一样以常规方式访问它。
request.getParameter("jsonObjectData");
现在您将拥有Stringified JSON,并且可以将json解析为Java对象
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句