4.6. Ajax Response Types

The most common server response types are:

Click Controls render themselves as XHTML markup so can be used in either XML or HTML responses.

Here is an example showing how to return different types of responses:

public class HelloWorldPage extends Page {

    ...

    public void onInit() {
        Behavior htmlBehavior = new DefaultAjaxBehavior() {
            public ActionResult onAction() {
                String html = "<h1>Hello world</h1>";

                // Return an HTML snippet
                return new ActionResult(html, ActionResult.HTML);
            }
        };
        htmlLink.addBehavior(htmlBehavior);

        ...

        Behavior xmlBehavior = new DefaultAjaxBehavior() {
            public ActionResult onAction() {
                String xml = "<payload>Hello world</payload>";

                // Return an XML snippet
                return new ActionResult(xml, ActionResult.XML);
            }
        };
        xmlLink.addBehavior(xmlBehavior);

        ...

        Behavior jsonBehavior = new DefaultAjaxBehavior() {
            public ActionResult onAction() {
                String json = "{\"value\": \"Hello world\"}";

                // Return an JSON snippet
                return new ActionResult(json, ActionResult.JSON);
            }
        };
        jsonLink.addBehavior(jsonBehavior);
    }
}