ajax - Retrieving Array of Object from params on Grails -


i'm passing array on post request using ajax call, not simple one, rather 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 controller

def orderservice; function savetransaction() {     def response = orderservice.save(params)     render response json } 

and 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 not display expect, instead displays following:

branches: null branches: branches: [] 

how can these passed branches on service params array / list?


after experimenting, i've seen not passed object rather flat map having it's accessor key, when use:

println "branches: " + branches."[0][address]" 

it prints:

branches: ... 

now, follow-up question how can change behavior instead?

println "branches: " + branches[0].address 

you might want use json format request, more appropriate 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   } } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -