How do I pass an array to a Spring controller method with jquery ajax

19,457

Solution 1

Try changing your RequestParam annotation value to this:

@RequestParam(value="objectValues[]", required=false)

If this solves the problem, then it is due to a parameter naming incompatibility between Spring and jQuery, where jQuery wants to put square brackets in to indicate that a parameter is an array (I think PHP likes this too), but where Spring doesn't care. To see the reverse try setting the "data" parameter of the ajax request to the string: 'objectValues=1234567890&objectValues=0987654321'

Solution 2

There are multiple ways to do this, depending on which component you think is sending or receiving data in the incorrect format (or which component you have access to modify).

If you believe the default way that jQuery sends data is correct, modify your controller appropriately (note you'll need to change both on the method signature and the annotation if you use both):

@RequestMapping(value = "/location", method=RequestMethod.GET, params="objectValues[]")  
public @ResponseBody String loadLocation(@RequestParam(value="objectValues[]", required=false) String[] objectValues) {  
    ...  
}

If you believe Spring functions correctly, but data sent from jQuery is incorrect, modify jQuery:

$.ajax({
    traditional: true,
    ...  
});

See more on jQuery AJAX settings for the traditional setting.

I myself think it is cleaner to modify the way jQuery sends data; it keeps my Controller syntax looking like I want.

Share:
19,457

Related videos on Youtube

coder
Author by

coder

Updated on May 27, 2022

Comments

  • coder
    coder almost 2 years

    Here's my ajax call:

     $.ajax({
         type: 'GET',
         url: contextPath + '/test/location',
         data: {'objectValues': object.objectValues },
         datatype: 'json',
         success: function( data ) {
         var obj = jQuery.parseJSON( data );
         }
       });
    

    it gives me this URL:

    http://localhost:8080/test/location?objectValues[]=1234567890&objectValues[]=0987654321

    My Spring method signature looks like this:

    @RequestMapping(value = "/location", method=RequestMethod.GET)
        public @ResponseBody String loadLocation(@RequestParam(value="objectValues", required=false) String[] objectValues)
    

    Why do I keep getting null for the value of objectValues?

  • coder
    coder about 13 years
    same result...since this is your suggestion, can I assume that everything else looks right?
  • Patricia
    Patricia about 13 years
    i've never worked with spring, but i had trouble with asp.net mvc and jquery not playing nice with arrays/objects. setting it to true makes the query string the same still?
  • Drazen Bjelovuk
    Drazen Bjelovuk over 9 years
    Alternatively, you can set traditional: true in your Ajax call.
  • gene b.
    gene b. almost 5 years
    Worked for me too... without the @RequestParam(value="array[]") it was setting a NULL into the String[] parameter.