In Grails we can get the response in the format we want i.e. JSON or XML, based on our request
To get the format oriented response:
- In URL-Mapping define the proper URL for your action
"/myUrl/$param(.${format})"(controller:"myController", action:"myAction", parseRequest:true)
- Now add data to a HashMap variable in your action to send it in a required format as response
HashMap res = new HashMap(); res.result = "success"; res.message = "Any message"; res.data = getListDataFromMethod(); respond res, [formats:['json', 'xml']];
- To get above response call this URL from ajax or hit it directly to browser
http://localhost:8080/myGrailsApp/myUrl/3.json
More From Oodles
Ready to innovate? Let's get in touch
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ashish Sharma
Ashish is a bright Groovy and Grails developer and have worked on development of various SaaS applications using Grails technologies. Ashish likes PC games and works out at Gym in his free time.