0

I'm attempting to make an ajax request to a very simple Grails method:

def countRace() {
    Date date = params.date("date", 'yyyy-MM-dd');
    Integer count = Race.countByDate(date);
    log.info("Number of races on " + date + " is " + count);
    return [count : count]
}

And I am using the following jQuery code to do it:

function updateDayRaceCount(id) {
    $.ajax({
       url: "${g.createLink(controller:'data', action:'countRace')}?date=" + id
    }).done(function(response) {
       alert(response);
    });
}  

I am seeing the logs fire on the server, so I know the method is being hit:

grails.app.controllers.runnerdb.DataController Number of races on Thu Mar 06 00:00:00 GMT 2014 is 38

But the ajax request is erroring out with a 404. When I observed the error, I found that it was the Tomcat standard 404 page, and the URL that was being generated, that looks like this:

/runnerdb/data/countRace?date=<id here>

Was being mapped to:

 /runnerdb/WEB-INF/grails-app/views/data/countRace.jsp

This is where the issue is, because I imagine Tomcat or grails are trying to render a non-existent view.

My question is..

How to I stop this kind of behaviour in grails for just this one method, and force it to simply return the int value?

1 Answer 1

3

By default Grails resolves views by convention. In your case you will need to use render instead of return. By using return you are telling Grails to continue the processing to a view using your return as the model.

Here is an example of rendering a text value:

render(text: count)

Alternatively you may want to render your results as JSON (provided you import the converter):

render [count: count] as JSON
Sign up to request clarification or add additional context in comments.

1 Comment

Instantly solved the issue. I swear, one day I'll get the hang of this!

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.