帖子JS数组对象为Spring MVC的控制器 [英] Post JS Object Array to Spring MVC controller

查看:120
本文介绍了帖子JS数组对象为Spring MVC的控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过多头排列,从jQuery的负载春天控制器。我怎么能发送JS对象数组T,Spring MVC中的控制器。

每一次行动发生在脚本中的任何数据警报会被调用。 剧本

  VAR ARR = []; //数组
    $(文件)。就绪(函数(){
        $(等级)。点击(函数(){
            。VAR IDX = $(本).closest('TD')指数();

            VAR userskill = {//对象
                高科技:$(本).closest(TD)的兄弟姐妹('td.tech)文本()。
                技巧:$('#listTable THEAD日),EQ(IDX)的.text()。
                右值:$(本).VAL()

            }
            加(userskill);
        });

    });


函数add(userskill){
    arr.push(userskill);
    $阿贾克斯({
        键入:POST,
        数据类型:JSON,
        网址:'/ SimplWebApp / saveUserRating',
        数据 : ({
            ID:JSON.stringify(ARR)
        }),
        成功:函数(responseData){
            如果(responseData!= NULL){

                警报(responseData);
            } 其他 {
                警报(无数据);
            }
        }

    });

}
 

控制器

  @RequestMapping(值=saveUserRating)
公共@ResponseBody字符串saveUserRating(@RequestParam(值=编号[],要求= FALSE)的String []×){
    GSON GSON =新GSON();
    字符串数据= gson.toJson(X);

    返回的数据;
}
 

解决方案

JSON数组驻留在请求的主体。您可以使用 @RequestBody 标注,以获得数组,如果您有杰克逊在类路径中反序列化JSON。

  saveUserRating(@RequestBody(需= FALSE)的String [] IDS)
 

如果你想使用数组作为响应体简单地从处理程序方法返回数组

。这将被序列自动JSON。

  @ResponseBody
公众的String [] saveUserRating(saveUserRating(@RequestBody(需= FALSE)的String [] IDS)){
    返回的ID;
}
 

I am trying to pass long array from jquery load to spring controller. How can i sent js object array t spring mvc controller.

Every time action occured the no data alert on script will called. Script

var arr = [];//Array
    $(document).ready(function() {
        $(".rating").click(function() {
            var idx = $(this).closest('td').index();

            var userskill = {//Object
                tech : $(this).closest('td').siblings('td.tech').text(),
                skill : $('#listTable thead th').eq(idx).text(),
                rValue : $(this).val()

            }
            add(userskill);
        });

    });


function add(userskill) {
    arr.push(userskill);
    $.ajax({
        type : 'POST',
        dataType : 'json',
        url : '/SimplWebApp/saveUserRating',
        data : ({
            id : JSON.stringify(arr)
        }),
        success : function(responseData) {
            if (responseData != null) {

                alert(responseData);
            } else {
                alert("no data");
            }
        }

    });

}

controller

@RequestMapping(value = "saveUserRating")
public @ResponseBody String saveUserRating(@RequestParam(value="id[]", required=false) String[] x) {
    Gson gson = new Gson();
    String data = gson.toJson(x);

    return data;
}

解决方案

The JSON array resides in the body of the request. You can use the @RequestBody annotation to obtain the array if you have Jackson on the classpath to deserialize the JSON.

saveUserRating(@RequestBody(required=false) String[] ids)

If you want to use the array as the response body simply return the array from the handler method. It will be serialized to JSON automatically.

@ResponseBody
public String[] saveUserRating(saveUserRating(@RequestBody(required=false) String[] ids)) {
    return ids;
}

这篇关于帖子JS数组对象为Spring MVC的控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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