On Wed, Jan 18, 2017 at 10:35 AM, James Selvakumar <ja...@mcruncher.com> wrote:
> Hi Martin, > > Is it possible to return a value from the ajax behavior while responding to > the ajax call? > I'm trying to do something like this in the server side: > > target.appendJavaScript("foo"); > > > > And then try to use it in my JavaScript: > > var foo = Wicket.Ajax.get({ u: window.callbackUrl }); > .get() makes an Ajax call, so it doesn't return a value You can use the callback methods (complete, success, failure) to get the returned response from the server But I think you don't need to use 'foo' from the server as a variable. You really need: target.appendJavaScript("doNextStep(foo)") I.e. execute something else once the Ajax call returns > > //do some processing with foo > > > > Is this valid? Or is there a better way? > > > > On Wed, Jan 18, 2017 at 5:26 PM, James Selvakumar <ja...@mcruncher.com> > wrote: > > > Thanks a lot Martin. You're right. I had something else in my code and > > after I fixed it, I managed to make a ajax call from JavaScript. > > > > On Wed, Jan 18, 2017 at 5:07 PM, Martin Grigorov <mgrigo...@apache.org> > > wrote: > > > >> There is no variable named 'a' in your code snippet. > >> It might be another problem. > >> > >> Martin Grigorov > >> Wicket Training and Consulting > >> https://twitter.com/mtgrigorov > >> > >> On Wed, Jan 18, 2017 at 10:04 AM, James Selvakumar <ja...@mcruncher.com > > > >> wrote: > >> > >> > Hi Martin, > >> > > >> > I tried your suggestion but I'm still getting the same TypeError. > >> > > >> > On Wed, Jan 18, 2017 at 4:55 PM, James Selvakumar < > ja...@mcruncher.com> > >> > wrote: > >> > > >> > > Thanks Martin for the super fast reply. I'll try that out. > >> > > > >> > > On Wed, Jan 18, 2017 at 4:54 PM, Martin Grigorov < > >> mgrigo...@apache.org> > >> > > wrote: > >> > > > >> > >> var wcall = Wicket.Ajax.get({ u: '${callbackUrl}'}); > >> > >> should be > >> > >> var wcall = Wicket.Ajax.get({ u: window.callbackUrl }); > >> > >> > >> > >> Martin Grigorov > >> > >> Wicket Training and Consulting > >> > >> https://twitter.com/mtgrigorov > >> > >> > >> > >> On Wed, Jan 18, 2017 at 9:51 AM, James Selvakumar < > >> ja...@mcruncher.com> > >> > >> wrote: > >> > >> > >> > >> > Dear Wicket team, > >> > >> > > >> > >> > I need to make a call to a Wicket page from my JavaScript > function. > >> > >> > I followed the guide Calling Wicket from Javascript > >> > >> > <https://cwiki.apache.org/confluence/display/WICKET/ > >> > >> > Calling+Wicket+from+Javascript> > >> > >> > and managed to define an ajax behavior to respond to the > JavaScript > >> > >> call. > >> > >> > > >> > >> > My ajax behavior looks like this: > >> > >> > > >> > >> > > private final AbstractDefaultAjaxBehavior behave = new > >> > >> > AbstractDefaultAjaxBehavior() > >> > >> > > { > >> > >> > > protected void respond(final AjaxRequestTarget target) > >> > >> > > { > >> > >> > > LOGGER.debug("Received a request from client to get the > >> > >> > encryption key"); > >> > >> > > target.add(new Label("foo", "Yeah I was just called > from > >> > >> > Javascript!")); > >> > >> > > } > >> > >> > > > >> > >> > > public void renderHead(Component component, IHeaderResponse > >> > >> response) > >> > >> > > { > >> > >> > > super.renderHead(component, response); > >> > >> > > String callbackUrl = getCallbackUrl().toString(); > >> > >> > > response.render(JavaScriptHeaderItem.forScript("var > >> > >> > callbackUrl='" + callbackUrl + "';", "values")); > >> > >> > > } > >> > >> > > }; > >> > >> > > > >> > >> > > This callback url got rendered in the page like this: > >> > >> > > >> > >> > <script id="values" type="text/javascript"> > >> > >> > > /*<![CDATA[*/ > >> > >> > > var callbackUrl='./wicket/page?0-1.IBehaviorListener.0-'; > >> > >> > > /*]]>*/ > >> > >> > > </script> > >> > >> > > > >> > >> > > >> > >> > > >> > >> > And here is my JavaScript code: > >> > >> > > >> > >> > > var wcall = Wicket.Ajax.get({ u: '${callbackUrl}'}); > >> > >> > > alert(wcall); > >> > >> > > > >> > >> > > But I'm getting the following error in the Wicket Ajax Debug > >> Window: > >> > >> > > >> > >> > *An error occurred while executing Ajax request:TypeError: a is > >> > >> undefined* > >> > >> > > > >> > >> > > >> > >> > Can someone point out what's the mistake here? > >> > >> > > >> > >> > -- > >> > >> > Thanks & regards > >> > >> > James > >> > >> > > >> > >> > >> > > > >> > > > >> > > > >> > > -- > >> > > Thanks & regards > >> > > James Selvakumar > >> > > > >> > > >> > > >> > > >> > -- > >> > Thanks & regards > >> > James > >> > > >> > > > > > > > > -- > > Thanks & regards > > James > > > > > > -- > Thanks & regards > James >