The front end sends an array of integer types to the server and reports an error
1. Front-end code
$(function () {
$.ajax({
url: "send/array/three.html",
type: "post",
contentType: "application/json;charset=UTF-8",
data: JSON.stringify([5,8,12]),
success: function (response) {
alert("成功"+response)
},
error: function (response) {
alert("失败"+response.toString())
}
})
});
2. Backend code
@RequestMapping("/send/array/three.html")
@ResponseBody
public String testReceiveArrayThree(@RequestBody Integer[] array) {
Logger logger = LoggerFactory.getLogger(TestHandler.class);
for (Integer number:array) {
logger.info("number="+number);
}
return "target";
}
3. The front end requests the back end to report an error
4. Reason: The mvc namespace of the SpringMVC configuration file is wrong
turn out to be:
After modification:
There are thousands of errors, the same error may be caused by multiple reasons, and there are possible reasons:
1. There is no guide package jackson
<!-- Spring 进行 JSON 数据转换依赖 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
2. The contentType does not conform to
3. The front-end request adds dataType: "json",