bug描述
.$ajax向控制层请求数据,控制层能够获取数据,在返回时出错,网页调试提示错误代码500.
前端代码
<script type="text/javascript">
function getHotTopic() {
$.ajax({
type:"get",
url:"${pageContext.request.contextPath}/topic/getHotTopic.do?size=2",
success:function (data) {
console.log(data);
alert("获取热点信息成功"+data);
location.reload();
},
error:function () {
alert("获取热点信息失败")
}
})
}
</script>
控制层代码
@RequestMapping("/getHotTopic")
@ResponseBody
public Map<String, Object> getHotTopic(Integer size){
Map<String, Object> result = new HashMap<>();
List<Topic> hotTopics = topicService.getHotTopic(size);
System.out.println(hotTopics);
result.put("hotTopics", hotTopics);
return result;
}
问题解决
在spring-mvc.xml中插入如下声明:
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
在pom.xml中导入如下依赖:
<!-- JSON配置 -->
<!-- JSON -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.6</version>
</dependency>
重新进行访问,可以看到问题得到解决,获得了后端的数据