{"id":7589,"date":"2012-09-16T12:33:16","date_gmt":"2012-09-16T07:03:16","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7589"},"modified":"2012-09-16T12:33:16","modified_gmt":"2012-09-16T07:03:16","slug":"implement-ajax-call-in-grails-web-flow","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/implement-ajax-call-in-grails-web-flow\/","title":{"rendered":"Implement Ajax call in Grails web-flow"},"content":{"rendered":"<p>In one of my recent project,  i want to use grails web-flow with ajax call. It&#8217;s very easy to implement the web-flow with ajax call. Grails web-flow always track the actions on the basis of <strong>eventId<\/strong> &amp; <strong>flow execution key<\/strong>. So, to implement ajax call in web-flow, we have to pass the event id &amp; flow execution key.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>1. Let us assume, we have the following web- flow code in grails.<\/p>\n<p>[groovy]<br \/>\ndef stepFlow ={<br \/>\nfirst{<br \/>\naction{<br \/>\ncode&#8230;..<br \/>\n}on (&quot;success&quot;){code&#8230;}.to(&quot;second&quot;)<br \/>\n}<br \/>\nsecond{<br \/>\non(&quot;third&quot;){<br \/>\n}.to(&quot;fourth&quot;)<br \/>\n}<br \/>\nfourth();<br \/>\n}<br \/>\n[\/groovy]<\/p>\n<p>In this case, <strong>step<\/strong> is web flow name, for each call we have to pass <strong>step<\/strong> as\u00a0<strong>request uri<\/strong> . First, second and third are events, events are always tracked through flow execution key<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>2. We want to call the event <strong>third<\/strong> through ajax call. For this we have to write the following code in ajax.<\/p>\n<p>[javascript]<br \/>\nfunction callAjaxFunctionInWebFlow(eventType,flowExecutionKey){<\/p>\n<p>$.ajax({<br \/>\nurl:&quot;project-name\/step&quot;,<br \/>\ntype:&#8217;POST&#8217;,<br \/>\ndata:&#8217;_eventId=&#8217; + eventType + &#8216;&amp;execution=&#8217; + flowExecutionKey,<br \/>\nsuccess:function (result) {<\/p>\n<p>},<br \/>\nerror:function (jqXHR, textStatus, errorThrown) {<br \/>\n}<br \/>\n})<br \/>\n}<br \/>\n[\/javascript]<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>3. To call this function from GSP we have to write the following code.<\/p>\n<p>[html]<br \/>\n&lt;a href=&#8217;javascript:void(0)&#8217; onclick=&quot;callAjaxFunctionInWebFlow(&#8216;third&#8217;,&#8217;${request.flowExecutionKey}&#8217;)&quot;&gt;Click&lt;\/a&gt;<br \/>\n[\/html]<\/p>\n<p>In this function <strong>callAjaxFunctionInWebFlow(&#8216;third&#8217;,&#8217;${request.flowExecutionKey}&#8217;) <\/strong><strong>third<\/strong> is <strong>eventId<\/strong> that we want to call,  <strong>request.flowExecutionKey<\/strong> gives the current execution key that need to pass through each ajax call.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>Hope it will help.<\/p>\n<p style=\"padding-bottom: 10px\">\n<p>Thanks &amp; Regards,<\/p>\n<p>Mohit Garg<\/p>\n<p>mohit@intelligrape.com<\/p>\n<p>@gargmohit143<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In one of my recent project, i want to use grails web-flow with ajax call. It&#8217;s very easy to implement the web-flow with ajax call. Grails web-flow always track the actions on the basis of eventId &amp; flow execution key. So, to implement ajax call in web-flow, we have to pass the event id &amp; [&hellip;]<\/p>\n","protected":false},"author":47,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":7},"categories":[7],"tags":[96,4840,9,27,971],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7589"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/47"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7589"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7589\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}