Implement Ajax call in Grails web-flow

16 / Sep / 2012 by Mohit Garg 3 comments

In one of my recent project, i want to use grails web-flow with ajax call. It’s very easy to implement the web-flow with ajax call. Grails web-flow always track the actions on the basis of eventId & flow execution key. So, to implement ajax call in web-flow, we have to pass the event id & flow execution key.

1. Let us assume, we have the following web- flow code in grails.

def stepFlow ={
first{
action{
code.....
}on ("success"){code...}.to("second")
}
second{
on("third"){
}.to("fourth")
}
fourth();
}

In this case, step is web flow name, for each call we have to pass step as request uri . First, second and third are events, events are always tracked through flow execution key

2. We want to call the event third through ajax call. For this we have to write the following code in ajax.

function callAjaxFunctionInWebFlow(eventType,flowExecutionKey){

$.ajax({
url:"project-name/step",
type:'POST',
data:'_eventId=' + eventType + '&execution=' + flowExecutionKey,
success:function (result) {

},
error:function (jqXHR, textStatus, errorThrown) {
}
})
}

3. To call this function from GSP we have to write the following code.

<a href='javascript:void(0)' onclick="callAjaxFunctionInWebFlow('third','${request.flowExecutionKey}')">Click</a>

In this function callAjaxFunctionInWebFlow(‘third’,’${request.flowExecutionKey}’) third is eventId that we want to call, request.flowExecutionKey gives the current execution key that need to pass through each ajax call.

Hope it will help.

Thanks & Regards,

Mohit Garg

mohit@intelligrape.com

@gargmohit143

FOUND THIS USEFUL? SHARE IT

comments (3)

  1. Jessie Hoeser

    You actually make it appear really easy along with your presentation however I to find this topic to be actually something which I feel I would never understand. It sort of feels too complicated and extremely broad for me. I’m looking forward for your subsequent submit, I’ll try to get the dangle of it!

    Reply
  2. Raj

    one question i have,

    So the ajax call needs data to be returned from webflow..

    How can we return data from webflow. I added some code in third step

    def stepFlow ={
    first{
    action{
    code…..
    }on (“success”){code…}.to(“second”)
    }
    second{
    on(“third”){

    def map= [:]
    map.id=’1′
    return map as json //render map as json

    }.to(“fourth”)
    }
    fourth();
    }

    Reply

Leave a comment -