Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
46
rated 0 times [  52] [ 6]  / answers: 1 / hits: 31868  / 13 Years ago, fri, november 25, 2011, 12:00:00

I have created a JS array like this var detailsArr = new Array(); and pushing some data into this array.



Now i push this array via Ajax to my Spring Controller like this



$.ajax({
type: POST,
url: submit,
data: ({detailsArr : detailsArr }),
success: function(html){
alert( Submitted);
}
});


At the Spring Controller side , i receive this array through the @RequestBody annotation. The Spring Controller method signature looks like this



public String submit(@RequestBody String body) 


But the array when received at the Spring Controller side is basically a String of this format



detailsArr[]=add&detailsArr[]=test1&detailsArr[]=test2&detailsArr[]=test3


I have to manually split this String to get the values, this is a cumbersome process. Is there any way in which I can get the array as it is , so that I just have to iterate over it to get the values.


More From » json

 Answers
7

you should pass your array to server in json format. And convert it by using Json to object converter. you can use Gson.



client side:



$.ajax({
type: POST,
url: submit,
data:JSON.stringify(detailsArr),
success: function(html){
alert( Submitted);
}
});


server side :



public String submit(@RequestBody String body){
//convert body to array using JSONLib, FlexJSON or Gson
}

[#88912] Wednesday, November 23, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;