Himanshu Yadav
Himanshu Yadav

Reputation: 13587

jQuery Ajax request in Grails

How to use jQuery to make Ajax request in Grails pages?

How to setup a URL hitting a method on Grails Controller? Let's say controller:'airport', action:'getJson' and input to the action is 'iata'.

I am able to set static url as http://localhost:8080/trip/airport/getJson but not able to figure out how to pass input for iata specifically.

I am fairly new to Grails and following IBM's 'Mastering the Grails' tutorial series. Do suggest me some good tutorial on using jQuery with Grails.

Upvotes: 4

Views: 13582

Answers (2)

Luis Muñiz
Luis Muñiz

Reputation: 4811

use the $.ajax method in jquery

$.ajax({
    url:"${g.createLink(controller:'airport',action:'getJson')}",
    dataType: 'json',
    data: {
        iata: '.............',
    },
    success: function(data) {
        alert(data)
    },
    error: function(request, status, error) {
        alert(error)
    },
    complete: function() {
    }
});

Upvotes: 11

Igor Artamonov
Igor Artamonov

Reputation: 35961

It's:

$.ajax({
  url: '/trip/airport/getJson',
  data: {paramName: 'iata'}
});

use your parameter name, that you're expecting in action, intead of paramName, that i've used.

Upvotes: 2

Related Questions