I'm passing an Array on a POST request using an AJAX call, but not a simple one, rather an Array of Objects:
var param = {
...,
branches: {
0: {
address: "...",
telephone: "...",
fax: "...",
...
},
...
nth: {
address: "...",
telephone: "...",
fax: "...",
...
}
}
}
$.ajax({
type: "POST",
url: ".../saveTransaction"
data: param
success: function(r) {
...
}
});
This is my controller
def orderService;
function saveTransaction() {
def response = orderService.save(params)
render response as JSON
}
And this is my service:
def save(params) {
def branches = params.branches
println "branches: $branches"
def branches = params.list("branches")
println "branches: $branches"
branches = params.list("branches[]")
println "branches: $branches"
}
It does not display what I expect, instead it displays the following:
branches: null
branches:
branches: []
How can I get these passed branches on the service from the params as an Array / List?
After experimenting, I've seen that it is not passed as an object rather as a flat Map having it's accessor as the key, so when I use:
println "branches: " + branches."[0][address]"
It prints:
branches: ...
Now, my follow-up question is how can I change this behavior to this instead?
println "branches: " + branches[0].address