17

I am developing a grails application which uses lot of ajax.If the request is ajax call then it should give response(this part is working), however if I type in the URL in the browser it should take me to the home/index page instead of the requested page.Below is the sample gsp code for ajax call.

<g:remoteFunction action="list" controller="todo" update="todo-ajax">

<div id ="todo-ajax">
//ajax call rendered in this area
</div>

if we type http://localhost:8080/Dash/todo/list in the browser URL bar, the controller should redirect to http://localhost:8080/Dash/auth/index

How to validate this in controller.

DonX
  • 16,093
  • 21
  • 75
  • 120

3 Answers3

34

It's quite a common practice to add this dynamic method in your BootStrap.init closure:

    HttpServletRequest.metaClass.isXhr = {->
         'XMLHttpRequest' == delegate.getHeader('X-Requested-With')
    }

this allows you to test if the current request is an ajax call by doing:

if(request.xhr) { ... }

The simplest solution is to add something like this to your todo action:

if(!request.xhr) { 
    redirect(controller: 'auth', action: 'index')
    return false
}

You could also use filters/interceptors. I've built a solution where I annotated all actions that are ajax-only with a custom annotation, and then validated this in a filter.

Full example of grails-app/conf/BootStrap.groovy:

import javax.servlet.http.HttpServletRequest

class BootStrap {

     def init = { servletContext ->

        HttpServletRequest.metaClass.isXhr = {->
            'XMLHttpRequest' == delegate.getHeader('X-Requested-With')
        }

     }
     def destroy = {
     }
} 
Siegfried Puchbauer
  • 6,539
  • 34
  • 24
  • Hi Siegfried , I tried with your answer.I added first part in bootstrap init closure.It is gives following exception No such property: HttpServletRequest for class: BootStrap:groovy.lang.MissingPropertyException: No such property:HttpServletRequest for class: BootStrap Could u please help me – DonX Apr 12 '09 at 03:07
  • Please provide ur email address also – DonX Apr 12 '09 at 03:09
  • 2
    You need to import the class javax.servlet.http.HttpServletRequest in your BootStrap class. I'll add a full example to the answer. – Siegfried Puchbauer Apr 12 '09 at 19:34
  • Thanks a lot.That small matter did not strike me. – DonX Apr 13 '09 at 08:00
  • 16
    This was added to Grails 1.1 - so if you are using that version or greater, then you can just use request.xhr now. – Chad Gorshing Jun 15 '10 at 13:51
7

Since Grails 1.1 an xhr property was added to the request object that allows you to detect AJAX requests. An example of it's usage is below:

def MyController {

  def myAction() {
    if (request.xhr) {
      // send response to AJAX request  
    } else {
      // send response to non-AJAX request
    }
  }
}
Dónal
  • 185,044
  • 174
  • 569
  • 824
3

The normal method is to have the ajax routine add a header or a query string to the request and detect that. If you're using a library for the ajax, it probably provides this already.

It looks like you're using prototype, which adds an X-Requested-With header set to 'XMLHttpRequest'; detecting that is probably your best bet.

Topera
  • 12,223
  • 15
  • 67
  • 104
MarkusQ
  • 21,814
  • 3
  • 56
  • 68