5

script Array of int and I wish to pass into Spring Controller. but I keep getting

400 bad request.

if my js array is

array = [1,2,3,4]
array -> 400 bad request
JSON.Stringify(array) -> I will get [1,2,3,4]

    $.ajax({//jquery ajax
                data:{"images": array},                
                dataType:'json',
                type:"post",
                url:"hellomotto"
                ....
            })

when I loop the string List.. the first element will be '[1'

@RequestMapping(value = "/hellomotto", method = Request.POST)
public void hellomotto(@RequestParam("images") List<String> images){
 sysout(images); -> I will get [1,2,3,4]
}

public void

May I know how can I do this properly? I have tried different combination

7
  • Please show what you're trying to pass it to and the stack trace from the server. Commented Oct 18, 2013 at 2:12
  • the development com is disconnected from internet.. I'm merely trying to pass an array of String/integer into the controller. Commented Oct 18, 2013 at 2:13
  • can you share the controller method? Commented Oct 18, 2013 at 2:15
  • Whether you're running on an Internet-connected computer is irrelevant; you're running an embedded Web server that just happens to be accessible to only one client, and it has stack traces that explain what failed. Commented Oct 18, 2013 at 2:22
  • Also, array -> 400 bad request is not very informative. Are you saying that you get that answer if you pass the array into that JQuery AJAX call? Commented Oct 18, 2013 at 2:24

3 Answers 3

7

The following is a working example:

Javascript:

$('#btn_confirm').click(function (e) {

    e.preventDefault();     // do not submit the form

    // prepare the array
    var data = table.rows('.selected').data();
    var ids = [];
    for(var i = 0; i < data.length; i++) { 
        ids.push(Number(data[i][0]));
    }

    $.ajax({
        type: "POST",
        url: "?confirm",
        data: JSON.stringify(ids),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data){
            alert(data);
        },
        failure: function(errMsg) {
            alert(errMsg);
        }
    });
});

Controller:

@RequestMapping(params = "confirm", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody int confirm(@RequestBody Long[] ids) {
    // code to handle request
    return ids.length;
}
Sign up to request clarification or add additional context in comments.

1 Comment

any idea how to do in angular 2, as it is passing array within array.
0

@RequestParam is used to bind request parameters, so if you do something like

@RequestMapping(value = "/hellomotto", method = Request.POST)
public void hellomotto(@RequestParam("image") String image){
     ...
}

and you do post to /hellomotto?image=test, the image variable in hellomotto method will contain "test"

What you want to do is to parse Request body , so you should you should use @RequestBody annotation :

http://docs.spring.io/spring/docs/3.0.x/reference/mvc.html#mvc-ann-requestbody

it is using jackson labrary (so you will have to include it as your dependency) to parse json object into java object.

Comments

0

I think you wantAjax call,through ajax, you are sending List of Integers so in spring your controller will be

@RequestMapping(value = "/hellomotto", method = Request.POST)
@ResponseBody
public void hellomotto(@RequestParam("images") List<Integer> images){
 sysout(images); -> I will get [1,2,3,4]
}

*@ResponseBody is missing in Your Code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.