JavaScript remoting in Visualforce provides support for methods in Apex controllers to be called via JavaScript.
[<namespace>.]<controller>.<method>([params...,] <callbackFunction>(result, event) {
// callback function logic
}, {escape:true});While the remote method call executes synchronously, it does not wait for the response to be returned. The response is handled asynchronously by the callback function, whenever the response completes. See Handling the Remote Response below for details.
@RemoteAction global static String getItemId(String objectName) { ... }
| @RemoteAction Scope | Visualforce Page | Non-Global Component | Global Component | iframe |
|---|---|---|---|---|
| Global Remote Method | Allowed | Allowed | Allowed | Allowed |
| Public Remote Method | Allowed | Allowed | Error | Error |
| Top Level Container | ||||
|---|---|---|---|---|
| @RemoteAction Accessed From | Visualforce Page | Non-Global Component | Global Component | iframe |
| Global Component | Allowed | Allowed | Allowed | Allowed |
| Non–Global Component | Allowed | Allowed | Only if non-global component doesn't include public remote methods. | Only if non-global component doesn't include public remote methods. |
| <apex:include><apex:composition> | Allowed, within the same namespace; error if namespaces are different, and the included page or its child hierarchy contains public remote methods. | n/a | n/a | Error |
Overloading is not permitted; in other words, your remote method can't have the same number of parameters as other methods with the same name. For instance, with the method above, you can't also have a getItemId(Integer objectNumber) method.
Your method can take Apex primitives, collections, typed and generic sObjects, and user-defined Apex classes as arguments. Generic sObjects must have an ID or sobjectType value to identify actual type. Your method can return Apex primitives, sObjects, collections, user-defined Apex classes and enums, SaveResult, UpsertResult, DeleteResult, SelectOption, or PageReference.
The response to a remote method call is handled asynchronously by the callback function defined in the remote method call. Your callback function will receive as parameters an event object representing the status of the remote call (true on success, false on error), and the result object returned by the remote Apex method. Your function can update information and user interface elements on the page based on the data returned.
Apex primitive data types returned by result—such as strings or numbers—are converted to their JavaScript equivalents. Apex objects that are returned are converted to JavaScript objects, while collections are converted to a JavaScript array. Keep in mind that JavaScript is case-sensitive, so id, Id, and ID are considered different fields.
As part of a JavaScript remote call, if the Apex method response contains references to the same object, the object won't be duplicated in the returned JavaScript object, and instead, the rendered JavaScript object will contain references to the same object. An example is an Apex method which returns a list that contains the same object twice.
The response of the remote call has a maximum size of 15 MB.
In general, <apex:actionFunction> is easier to use and requires less code, while JavaScript remoting offers more flexibility.