[英]Spring MVC @RequestBody not working with jquery ajax?
Here is my ajax request 这是我的ajax请求
var dataModel = {name1:"value1", name2:"value2"};
$.ajax({
url: "/testURL",
type: "POST",
async: false,
contentType: "application/json",
data: dataModel,
success: function(response) {
}
})
Here is my relevant snippet from spring xml 这是我从Spring XML的相关片段
<annotation-driven>
<!-- Message converters are added to use custom object mapper in MappingJackson2HttpMessageConverter.
StringHttpMessageConverter is required to avoid MappingJackson2HttpMessageConverter from converting a string into json.
-->
<message-converters>
<beans:bean
class="org.springframework.http.converter.StringHttpMessageConverter">
</beans:bean>
<beans:bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<beans:property name="objectMapper" ref="jacksonObjectMapper"/>
</beans:bean>
</message-converters>
</annotation-driven>
Here is my controller mapping 这是我的控制器映射
@RequestMapping(value = "/testURL", method = { RequestMethod.POST })
public String add(HttpServletRequest request, @RequestBody CustomObject customObject) throws Exception {}
But My request does not even reach to controller. 但是我的请求甚至没有到达控制器。 As soon as I remove @RequestBody CustomObject customObject
it works. 一旦删除@RequestBody CustomObject customObject
它就会起作用。 But I want to map the json request to CustomObject with @RequestBody
which is not happening . 但是我想用@RequestBody
将json请求映射到CustomObject,这没有发生。 Not sure what i am missing here ? 不知道我在这里想念什么吗?
In fact when I inspect request.getParameterMap()
it displays empty but as soon as I remove contentType: "application/json"
I see parameter map gets populated but still then get below error 实际上,当我检查request.getParameterMap()
它显示为空,但是一旦删除contentType: "application/json"
我看到参数映射已填充,但仍然低于错误
`The server refused this request because the request entity is in a format not supported by the requested resource for the requested method`
Here is my CustomObject definition 这是我的CustomObject定义
public class CustomObject implements Serializable {
private static final long serialVersionUID = 1L;
private String name1;
private String name2;
//getters and setters
}
Already gone through JQuery, Spring MVC @RequestBody and JSON - making it work together but did not help 已经通过JQuery,Spring MVC @RequestBody和JSON-使其可以一起使用但无济于事
In fact when I inspect request.getParameterMap() it displays empty but as soon as I remove contentType: "application/json" 实际上,当我检查request.getParameterMap()时,它显示为空,但是一旦我删除contentType:“ application / json”
That is right. 没错 Reason is with contentType: "application/json"
jquery internally convert the data into string. 原因与contentType: "application/json"
jQuery在内部将数据转换为字符串。 so there is no request parameter. 因此没有请求参数。 Without contentType: "application/json"
, default contentType' is form data . So data sent is converted to request parameters based on delimiters
如果没有contentType: "application/json"
,则默认的contentType' is form data . So data sent is converted to request parameters based on delimiters
contentType' is form data . So data sent is converted to request parameters based on delimiters
& and
=` contentType' is form data . So data sent is converted to request parameters based on delimiters
& and
=` contentType' is form data . So data sent is converted to request parameters based on delimiters
Also try data: JSON.stringify(dataModel)
, it should work 还尝试data: JSON.stringify(dataModel)
,它应该可以工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.