How to send javascript array to struts action by using jQuery Ajax

前端 未结 2 1450
北恋
北恋 2021-01-13 16:22

I am new to Struts 2. I want to send a javascript array to a Struts action class by using jQuery AJAX request.
The alert is working fine, the execute() is

2条回答
  •  北海茫月
    2021-01-13 16:33

    Jquery serializes data sent as parameters using $.param internally when doing ajax request with $.ajax.

    The data should be set as array of integers or string with comma separated list of integers, so jQuery can correctly serialize it before sending with the request.

    You can send an array parameter to struts2 only with traditional setting because struts using type conversion to populate a property of the action using keys as parameter names.

    So, the array should be an array of primitive integers but your array contains other objects that are not primitive integers.

    To demonstrate you can see this demo to understand how to get parameter values and serialize it the same way like is doing $.ajax.

    Struts2 also can convert a string containing a comma separated values by default type conversion. For example you can see how checkbox list values are passed to struts action.

提交回复
热议问题