3

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 \?

1 Answer 1

1

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'}"));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.