tags:

views:

205

answers:

0

I have a filter setup to get executed for all actions and it is executed once per request unless ajax get request is sent to the server. When it's an ajax get request, the filter gets executed twice but I see the browser sending only one request to the server. Is there a way to get the filter to fire only once per request?

Here's how my filter is setup:

class FrontFilters {
   def filters = {
      // filter on all controllers and actions
      first_filter(controller: '*', action: '*') {
         before = {
            println "=>(first_filter), Controller: ${controllerName}, action: ${actionName}, params: ${params}";
            return true;
         }
      }
   }
}

In my demo/index.gsp, I have the following inside the document "head" tag:

<script type="text/javascript">
       $(document).ready(function()
       {
           var url = "http://localhost/filtertest/demo/dutch";
           $.get(url, function(txt) {
                console.log("Hello there. I finished.");
            });
       });
</script>

My action is defined in a controller named 'DemoController':

def index = {
    println "Demo/index action reached";
    render(view: "index");
}

def dutch = {
    log.info("Demo/dutch action reached. Returning text/json render type");
    return render(contentType:'text/json') { 'aPair'('myKey': "myValue") };
}

The server log after http://localhost/filtertest/demo/ is invoked:

=>(first_filter), Controller: demo, action: null, params: [controller:demo]
Demo/index action reached

=>(first_filter), Controller: demo, action: dutch, params: [action:dutch, controller:demo]
Demo/dutch action reached. Returning text/json render type

=>(first_filter), Controller: demo, action: dutch, params: [action:dutch, controller:demo]
Demo/dutch action reached. Returning text/json render type

Note that the 2 lines were generated for one request for the action 'dutch' whereas I was only expecting the 'dutch' action related items to get printed only once.

I don't understand who is initiating the second filter execution.