How to pass JSON from wicket to JavaScript?
I have the following function in java script page that call wicket ajax:
function callWicketPage() {
wicketAjaxGet(
urlCallback,
function() {alert('success'); },
function() { alert('failed');
});
}
and in the wicket page I do the following:
final AbstractDefaultAjaxBehavior behave = new AbstractDefaultAjaxBehavior() {
@Override
public void renderHead(IHeaderResponse ihr) {
super.renderHead(ihr);
ihr.renderJavascript("var开发者_运维问答 urlCallback = '" + this.getCallbackUrl() + "';", "insertedjavascript");
}
protected void respond(final AjaxRequestTarget target) {
}
};
add(behave);
What i want is to send back a json response from the wicket page to JavaScript, How could i do it \?
i would suggest you to use a ResourceReference
instead, so you don't have to get read/write access to page instance (and mount it for easier access from your client side script).
But if you want to keep your behavior based solution:
class JsonBehavior extends AbstractDefaultAjaxBehavior {
@Override
protected void updateAjaxAttributes(AjaxRequestAttributes attributes) {
super.updateAjaxAttributes(attributes);
// please define the channel type (default: queue)
attributes.setChannel(new AjaxChannel("json"));
}
@Override
protected void respond(AjaxRequestTarget target) {
RequestCycle rc = RequestCycle.get();
rc.replaceAllRequestHandlers(new TextRequestHandler("application/json", "UTF-8", "{'json':'content'}"));
}
}
精彩评论