JavaScript Remoting

Use JavaScript remoting in Visualforce to call methods in Apex controllers from JavaScript. Create pages with complex, dynamic behavior that isn’t possible with the standard Visualforce AJAX components.

Features implemented using JavaScript remoting require three elements:
  • The remote method invocation you add to the Visualforce page, written in JavaScript.
  • The remote method definition in your Apex controller class. This method definition is written in Apex, but there are some important differences from normal action methods.
  • The response handler callback function you add to or include in your Visualforce page, written in JavaScript.
In your controller, your Apex method declaration is preceded with the @RemoteAction annotation like this:
@RemoteAction
global static String getItemId(String objectName) { ... }
Apex @RemoteAction methods must be static and either global or public.
A simple JavaScript remoting invocation takes the following form.
[namespace.]controller.method(
    [parameters...,]
    callbackFunction,
    [configuration]
);
Table 1. Remote Request Elements
Element Description
namespace The namespace of the controller class. This is required if your organization has a namespace defined, or if the class comes from an installed package.
controller The name of your Apex controller.
method The name of the Apex method you’re calling.
parameters A comma-separated list of parameters that your method takes.
callbackFunction The name of the JavaScript function that will handle the response from the controller. You can also declare an anonymous function inline. callbackFunction receives the status of the method call and the result as parameters.
configuration Configures the handling of the remote call and response. Use this to change the behavior of a remoting call, such as whether or not to escape the Apex method’s response.

For more information, see “JavaScript Remoting for Apex Controllers” in the Visualforce Developer's Guide.

Previous
Next