Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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;
}
share|improve this question

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.