2

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
1
  • Can you paste here the params that your getting in controller Commented Jun 13, 2015 at 9:23

2 Answers 2

2

you might want to use JSON format for your request, which is more appropriate for your data structure:

$.ajax({
    type: "POST",
    url: ".../saveTransaction",
    dataType:'json',
    data: param     
});


class YourController {
  def save(){
    def json = request.JSON
    def list = json.branches
    service.save list
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry for a late reply. This doesn't work, just tested this today, println "JSON: $json" displays: JSON: [:].
you request must be empty
How can you pass value to request? Isn't it just the same with params that is automatically generated when a request was made to the controller?
1

Based on Igor's anser, I changed my AJAX call, used the JSON.stringify function and contentType option:

$.ajax({
    type: "POST",
    url: saveAgreementURL,
    data: JSON.stringify(data),
    contentType: 'application/json',
    success: function(r) {
    }
});

And on my controller, I used this:

def orderService;
function saveTransaction() {
    def response = orderService.save(request.JSON)
    render response as JSON
}

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.