Page Actions are page methods that can be invoked directly from the browser. So instead of handling the Ajax request with a Control, the request is handled with a page method.
Similar to the AjaxBehavior onAction
method,
page methods returns an ActionResult
containing the data to render to the browser.
Page Actions have been covered earlier. Please click here for a detailed overview.
Using a Page Action for handling an Ajax request is no different from
the normal HTTP version. To invoke a Page Action, specify the parameter
"pageAction"
and the name of the page method eg:
"onLinkClicked".
Here is an example using the jQuery JavaScript library to make an Ajax request to a Page Action:
jQuery('#some-link-id').click(function() { // The ViewCustomerPage url var url = '$context/view-customers.htm'; // Specify the pageAction parameter and page method to invoke: 'onLinkClicked' var extraData = 'pageAction=onLinkClicked'; // Perform the Ajax request jQuery.get(url, extraData, function(response) { // Update the target element with the server response jQuery("#target").html("<p>" + response + "</p>"); }); });
The JavaScript snippet above will send a request to the
ViewCustomerPage
method "onLinkClicked",
which returns an ActionResult instance:
public class ViewCustomerPage extends Page { ... public ActionResult onLinkClicked() { // Formatted date instance that will be returned to the browser String now = format.currentDate("MMM, yyyy dd HH:MM:ss"); String msg = "PageAction method <tt>onLinkClicked()</tt> invoked at: " + now; // Return an action result containing the message return new ActionResult(msg, ActionResult.HTML); } }
The ActionResult contains the data that is rendered to the client browser. In the example above, the action result is an HTML snippet containing todays date.