我需要在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 sending form data and a json object in an ajax call我也尝试过相同的方式。
如何在单个ajax请求中发送FormData和JSON对象?
答案 0 :(得分:0)
你可以通过这种方式传递
postdata={};
postdata.formData=formData;
postData.jsonData=jsonData
$.ajax({
type: "POST",
url: postDataUrl,
data: { postdata},
processData: false,
contentType: false,
async: false,
cache: false,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
success: function(data, textStatus, jqXHR) {
}
});
和控制器端,您可以识别数据。
答案 1 :(得分:0)
我通过在ajax POST中附加带有FormData对象的JSON字符串来解决它。
driver.findElement(By.xpath("(//*[@class = 'msgContent']/descendant::a[contains(@class,'btn assign')])[1]")).click();
并且在控制器中,您可以像访问其他表单数据值一样以正常方式访问它。
var jsonObjectData = JSON.stringify(jcArray);
formData.append("jsonObjectData",jsonObjectData);
现在你将拥有Stringified JSON,你可以将json解析为Java对象
答案 2 :(得分:0)
您可以执行以下操作以在Ajax调用中传递表单数据。
var formData = $('#client-form').serialize();
$.ajax({
url: 'www.xyz.com/index.php?' + formData,
type: 'POST',
data:{
},
success: function(data){},
error: function(data){},
})