Reputation: 73
I've managed to replicate this w3schools example for jQuery AJAX using Resource mounting in Wicket.
However, I still want to know if there are better ways of handling arbitrary AJAX requests.
Upvotes: 0
Views: 125
Reputation: 876
If you want low-level control over what you're sending. AbstractAjaxBehavior
is probably more or less what you're looking for. You can do something like
public class MyAjaxBehavior extends AbstractAjaxBehavior{
@Override
public void onRequest() {
RequestCycle.get().scheduleRequestHandlerAfterCurrent(
new TextRequestHandler("application/json", "UTF-8", "{myVal:123}")
);
}
};
You can replace the TextRequestHandler
with some other request handler (or write your own) to return exactly the data you want.
If you wish to write your own javascript handling the ajax you can look at the Wicket Ajax wiki page, where it describes how this ajax is to be invoked manually (you can get the callback URL via AbstractAjaxBehavior#getCallbackUrl()
).
If all you really want is just updating the display - high-level ajax for updating wicket components is what you want. Most Wicket Ajax behaviors (like AjaxEventBehavior
) attach their own ajax invocation, you don't have to do the javascript side of things at all. All you need to do is add components to the provided AjaxRequestTarget
, and wicket will do the rest, updating what the component displays automatically.
Both of these methods will access the page however, and if that is not what you want, resource mounting is the way to go.
Upvotes: 1