V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
hastyfish
V2EX  ›  问与答

$.get() success 的参数到底是个什么玩意

  •  
  •   hastyfish · May 29, 2019 · 2020 views
    This topic created in 2524 days ago, the information mentioned may be changed or developed.
    $.get(
                    "/getItem?opt=getItems&cName="+cName+"&rnd=" + Math.random().toString(),
                    {},
                    function (resp) {
                        console.log(resp);
                        for (var i in resp) {
                            $("#ilist")[0].options.add(new Option(resp[i][0].itemsName, resp[i][0].itemsName));
                        }
                        if (resp[i].length > 0)
                            $("#ilist")[0].selectedIndex = 0;
                        display();
                    },
                    "json"
                )
    

    后台

    @RequestMapping("/getItem")
        @ResponseBody
        public List<List<Items>> getItem(@RequestParam("cName") String cName){
            return categoriesService.getItems(cName);
        }
    

    用 resp[i].itemsName 取不到值,resp[i][0].itemsName 才有值
    看了下控制台输出的 resp

    0: Array(1)
       0:{正常数据}
       length: 1
       _proto_:Array(0) 底下有一堆
       ...
       
    1:Array(1)
       0:{正常数据}
       length: 1
       _proto_:Array(0)
       ...
    

    难道是 java 中 Array 的属性吗

    6 replies    2019-05-30 09:36:38 +08:00
    randyo
        1
    randyo  
       May 29, 2019 via Android
    前段把拿到的数据打印一下,后端把返回的数据打印一下,看看是不是一样的。应该是后端封装的数据就是这样的。
    hastyfish
        2
    hastyfish  
    OP
       May 29, 2019 via Android
    @randyo
    不一样,用 jackson 把 List 转成 JSON 格式
    wa8n
        3
    wa8n  
       May 29, 2019 via iPhone
    你的方法返回的就是 List<List<Items>>啊
    hastyfish
        4
    hastyfish  
    OP
       May 29, 2019 via Android
    @wangsongyan
    这个 List<List<Items>>可以看成 Map, 实际效果也是一样的,根据后台的结果应该可以用 resp[i].对象名直接获得,我这个是一对多查询用 List<List<Items>>处理了下
    randyo
        5
    randyo  
       May 30, 2019 via Android
    list 就是数组啊,你数组里面套数组不就是这样子?
    jifengg
        6
    jifengg  
       May 30, 2019 via Android
    后端的问题,不是前端的问题
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5457 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 09:31 · PVG 17:31 · LAX 02:31 · JFK 05:31
    ♥ Do have faith in what you're doing.