AJAX/JQUERY访问Controller返回各种JSON形式

package com.example.demo.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.pojo.Student;

@RestController
public class JsonController {

    @RequestMapping("/j1")
    public ModelAndView j1() {
        return new ModelAndView("j1.html");
    }

    @RequestMapping("/j2")
    public String j2() {
        return "hello";
    }

    // 返回MAP形式转换的JSON
    @RequestMapping("/j3")
    public Map<String, Object> j3() {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("hello", 1);
        map.put("world", 1.5f);
        map.put("foo", true);
        map.put("bar", "ok");

        return map;
    }

    // 返回对象形式转换的JSON
    @RequestMapping("/j4")
    public Student j4() {

        Student student = new Student();
        student.setId(1);
        student.setName("smith");
        student.setWeight(185.5f);
        student.setMale(true);
        student.setLikes(Arrays.asList("football", "basketball", "volleyball"));

        return student;
    }

    // 返回List套Map形式转换的JSON
    @RequestMapping("/j5")
    public List<Map<String,Object>> j5() {

        List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("hello", 1);
        map.put("world", 1.5f);
        map.put("foo", true);
        map.put("bar", "yes");
        
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("hello", 12);
        map2.put("world", 12.5f);
        map2.put("foo", false);
        map2.put("bar", "no");
        
        list.add(map);
        list.add(map2);

        return list;
    }

    // 返回List套对象形式转换的JSON
    @RequestMapping("/j6")
    public List<Student> j6() {

        List<Student> students=new ArrayList<Student>();
        Student student = new Student();
        student.setId(1);
        student.setName("smith");
        student.setWeight(185.5f);
        student.setMale(true);
        student.setLikes(Arrays.asList("football", "basketball", "volleyball"));
        
        Student student2 = new Student();
        student2.setId(2);
        student2.setName("sarah");
        student2.setWeight(165.5f);
        student2.setMale(false);
        student2.setLikes(Arrays.asList("shopping", "market", "walk"));
        
        students.add(student);
        students.add(student2);
        

        return students;
    }

}
 

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 引入jquery框架 方便ajax开发 -->
<script type="text/javascript" src="js/jquery-1.8.2.min.js">
</script>
<script type="text/javascript">

    //访问响应url的controller并返回三string
    function ajaxPost2(url){
        $.post(url,function(data){
            alert(data);
        });
    }
    
    //访问响应url的controller并返回MAP转换的json字符串
    function ajaxPost3(url){
        $.post(url,function(data){
            
            //true参与运算相等于1
            alert(data['hello']+data['world']+data['foo']+data['bar']);
            //两者有区别
            alert(data['hello']+" "+data['world']+" "+data['foo']+" "+data['bar']);
            
        });
    }
    
    //访问响应url的controller并返回对象转换的json字符串
    function ajaxPost4(url){
        $.post(url,function(data){
            
            //true参与运算相等于1
            //alert(data['id']+data['name']+data['weights']+data['male']);
            //两者有区别
            alert(data['id']+" "+data['name']+" "+data['weight']+" "+data['male']);
            
            var t="";
            
            //普通循环
/*             for(var i=0;i<data['likes'].length;i++){
                t+=data['likes'][i]+" ";
            }; */
            
            //forEach循环
/*             data['likes'].forEach(function(like){
                t+=like+" ";
            }); */
            
            //Lambda写法
            data['likes'].forEach(like=>t+=like+" ");
            alert(t);
        });
    }
        

    
    //访问响应url的controller并返回对象转换的json字符串
    function ajaxPost5(url){
        $.post(url,function(datas){
            
            datas.forEach(function(data){
                alert(data['hello']+" "+data['world']+" "+data['foo']+" "+data['bar']);
            });
        });
    }
    
    //访问响应url的controller并返回List套对象形式转换的JSON
    function ajaxPost6(url){
        $.post(url,function(datas){
            var t="";
            datas.forEach(function(data){
                data['likes'].forEach(like=>t+=like+" ");
                alert(data['id']+" "+data['name']+" "+data['weight']+" "+data['male']+" "+t);
                t="";
            });
        });
    }
    
</script>
</head>
<body>
用框架自动转换JSON

<br>
<input type="button" οnclick="ajaxPost2('j2')" value="返回字符串"/><BR>
<input type="button" οnclick="ajaxPost3('j3')" value="返回MAP转换JSON"/><BR>
<input type="button" οnclick="ajaxPost4('j4')" value="返回对象转换JSON"/><BR>
<input type="button" οnclick="ajaxPost5('j5')" value="返回List套Map形式转换的JSON"/><BR>
<input type="button" οnclick="ajaxPost6('j6')" value="返回List套对象形式转换的JSON"/><BR>

</body>
</html>

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值