springMvc如何返回JSON数据

桃扇骨 2022-03-29 18:41 457阅读 0赞

1.在pom.xml引用jar

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.core</groupId>
  3. <artifactId>jackson-databind</artifactId>
  4. <version>2.9.5</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.fasterxml.jackson.core</groupId>
  8. <artifactId>jackson-core</artifactId>
  9. <version>2.9.5</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>com.fasterxml.jackson.core</groupId>
  13. <artifactId>jackson-annotations</artifactId>
  14. <version>2.9.5</version>
  15. </dependency>

2.配置springMvc配置文件dispatcher-servlet.xml
在这里插入图片描述
3.创建一个Controller,在方法上配置@ResponseBody注解
在这里插入图片描述
4.用ajax发送请求返回JSON数据

  1. <script type="text/javascript">
  2. $(document).ready(function () {
  3. $("input").click(function () {
  4. $.ajax({
  5. url:"/helloSpringMvc/hello",
  6. type:"get",
  7. dataType : "json",
  8. success : function(result) {
  9. alert(result.name+"姓名");
  10. alert(result.sex+"性别");
  11. }
  12. })
  13. })
  14. })
  15. </script>
  16. <input type="button" value="点击"/>

发表评论

表情:
评论列表 (有 0 条评论,457人围观)

还没有评论,来说两句吧...

相关阅读