序列化javascript数组 [英] serialize javascript array

查看:83
本文介绍了序列化javascript数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用dojo增强型网格,标记为"myGrid"的变量引用该网格.

I am using the dojo enhanced grid and the variable marked "myGrid" refers to the grid.

function addtocart() {
        $("#additem").click(function(){
            var myGrid = dojo.byId(dataGrid);
            var ids = [];
            var gridRow = myGrid.selection.getSelected();
            $.each( gridRow, function(i, l){
                ids.push(l.id);
            });
            var registcarturl = "${carturl}" + $("#regCart :selected").val();
            $.get(registcarturl, {instanceIds: ids}, function(data) {
                alert(data);
            });
        });
    }

我不确定序列化数组的字符串应该是什么样子,因为我可以动态生成任何字符串.我正在尝试使用示例中所示的jquery get方法

I am not sure what the string for a serialized array should look like as I can dynamically build any string. I am trying to use the jquery get method as shown in the example

$.get("test.cgi", { name: "John", time: "2pm" },   function(data){     alert("Data Loaded: " + data);   });

我对功能进行了如下修改

I modified the function as follows

    function addtocart() {
        $("#additem").click(function(){
            var myGrid = dojo.byId(dataGrid);
            var ids = "[";
            var gridRow = myGrid.selection.getSelected();
            $.each( gridRow, function(i, l){
                ids = ids + "\"" +l.id +"\"";

                if(i != (gridRow.length -1)){
                    ids = ids + ",";
                };

            });
            ids = ids + "]";
            alert(ids);
            var registcarturl = "${carturl}" + $("#regCart :selected").val();
            $.get(registcarturl, {instanceIds: ids}, function(data) {
                alert(data);
            });
        });
    }

当我的数据到达Java控制器时,它会尝试解析 ["219" 很长时间,当然会导致NumberFormatException. javascript变量 ids 看起来像这样 ["219","217","218","195"]

When my data gets to my java controller it tries to parse ["219" as a long and of course a NumberFormatException results. The javascript variable ids looks like this ["219","217","218","195"]

有人可以提供一些指导吗?

Can someone please offer some guidance.

推荐答案

好,所以解决方案非常简单.在这里.

Ok so the solution was very very simple. Here it is.

    function addtocart() {
        $("#additem").click(function(){
            var myGrid = dojo.byId(dataGrid);
            var ids = [];
            var gridRow = myGrid.selection.getSelected();
            $.each( gridRow, function(i, l){
                ids[i] = l.id;
            });
            var registcarturl = "${carturl}" + $("#regCart :selected").val();
            $.get(registcarturl, {"modinstid[]": ids}, function(data) {
                alert(data);
            });
        });
    }

查看获取请求{"modinstid[]": ids},相应的spring MVC控制器如下.

Look at the get request {"modinstid[]": ids} and the corresponding spring MVC controller is as follows.

@RequestMapping(value = "addtocart/{cart}", method = RequestMethod.GET) public @ResponseBody
String addtocart(@PathVariable("cart") Long cart, @RequestParam("modinstid[]") ArrayList<String> instances, Model uiModel) {...Action logic goes here...}

查看操作参数的 @RequestParam 属性

这篇关于序列化javascript数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆